I have two routes for web middleware in my laravel app listed below:
Route::get('package/{slug}','GetPublicController#tourDetail')
->name('single');
And
Route::get('/trips/{category}','GetPublicPageController#getBycategory')
->name('getBycategory');
The first route works well but second route doesn't. The second route is in conflict with the first route as it tries to get view of first route, causing error and app to breakdown.
Method for 1st route:
public function tourDetail($slug)
{
$tour = Tour::where('slug', '=', $slug)
->first();
$itineraries = Itinerary::where('tour_id','=', $tour->id)
->orderBy('id', 'asc')->get();
$depature_dates = $tour->datePrice()->FixedDates($tour->id, date('m'),date('Y'))->get();
return view('public.tour.tour-detail')
->withTour($tour)
->withItineraries($itineraries)
->withDepatures($depature_dates);
}
And method for 2nd route
public function getByCategory($category)
{
$query = Tour::whereHas('category', function($r) use($category) {
$r->where('tcategories.name','=', $category);
});
return view('public.pages.category-list')
->withResults($query);
}
It would be very helpful if anyone could suggest the best approach to resolve this issue.
Thank you.
You have to call ->get() on the query in getByCategory.
Related
I have modified default auth method in controller which redirects user after custom login to set_password page. The problem is I can redirect it well to the desired page but I need to simultaneously pass two dynamic variables which are returned through querying database, which I am unable to pass with redirect.
My modified controller method is as follows:-
protected function authenticated(Request $request, $user)
{
$activated_up = User::where('id_user',Auth::user()->id_user)
->where(function($query) {
$query->where('activated_up', '=', '1')
->orWhere('activated_up', '=','0');
})
->get(['activated_up']);
$showuser = UserProfile::where('id_user',Auth::user()->id_user)->first();
return redirect()->route('set_password',['activated_up' => $activated_up, 'showuser' => $showuser]);
}
I know that to pass a variable to an view, I need to use the compact method like follows:-
return view('set_password', compact('activated_up', 'showuser'); but it cant be done with redirect.
The way I have redirected means I am passing parameters to route in the controller method, but I need to pass variables to the redirected view instead of parameters. How to achieve that?
protected function authenticated(Request $request, $user)
{
$activated_up = User::where('id_user',Auth::user()->id_user)
->where(function($query) {
$query->where('activated_up', '=', '1')
->orWhere('activated_up', '=','0');
})
->get(['activated_up']);
$showuser = UserProfile::where('id_user',Auth::user()->id_user)->first();
return redirect()->route('set_password',['activated_up' => $activated_up, 'showuser' => $showuser]);
}
you can use With
return redirect()->route('set_password')->with('data', ['some kind of
data']);
in your view
#if (session::has('data'))
The data is {{ session::get('data') }}
#endif
Tried as described in the answer by Kuldeep Mishra but could not achieve it though, anyways I found a workaround to achieve my desired output. What I did is changed my authenticated method to this:-
protected function authenticated(Request $request, $user)
{
return redirect()->route('set_password');
}
I only redirected to the set_password route from the above method and made new method in the controller to show the view with the compacted variables like this:-
public function setPasswordForm(Request $request)
{
$activated_up = User::where('id_user',Auth::user()->id_user)
->where(function($query) {
$query->where('activated_up', '=', '1')
->orWhere('activated_up', '=','0');
})
->get(['activated_up']);
$showuser = UserProfile::where('id_user',Auth::user()->id_user)->first();
return view('set_password', compact('activated_up', 'showuser'));
}
And a small change in route web.php file:-
Route::get('/set_password', 'Controller#setPasswordForm')->name('set_password');
So finally I was able to redirect to desired page with the desired view loaded with dynamic variables.
I want redirect url from /topic/{title} to /topic/{category}/{title}.
So I try to registered this in routes:
Route::get('/topic/{title}',function(){
return redirect()->action('/topic/{category}/{title}','DetailController#index');
});
But I got error
Action App\Http\Controllers/topic/{category}/{title} not defined.
Anyone can help me about the routes?
Thanks for advance.
This is my Controller
class DetailController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index($section=0,$title = 0)
{
$detail = DB::table('t_artikel')
->join('t_section', 't_section.id_section', '=', 't_artikel.id_section')
//->join('t_kolom', 't_kolom.id', '=', 't_artikel.penalar')
->where('publish', '=', 'Y')
->where('parent_id', '=', 0)
->where('tgl_pub', '<=', date('Y-m-d H:i:s'))
->where('t_artikel.urltitle', '=', $title)
->select('t_artikel.*', 't_section.*', 't_artikel.urltitle as urlartikel')
->first();
$kategori = DB::table('t_section')
->where('id_supsection', '=', 0)
->where('status', '=', 1)
->orderBy('position', 'asc')
->whereNotIn('id_section', [34])
->select('t_section.*')
->get();
$page = 'Detail';
return view ('detail.detail',
['detail' => $detail,
'kategori' => $kategori,
'page' => $page
]);
}
As per the documentation,
If your controller route requires parameters, you may pass them as the second argument to the action method
So, you need to pass the parameters like this:
return redirect()->action(
'DetailController#index', ['category' => <enter_value>, 'title' => <enter_value>]
);
I would suggest you to use 2 routes and 2 controllers. One handles the details of the topic and the other one handles the redirect of the "old" url.
For example: the user will visit "/topic/title" that is handled by a controller that will recognize topic and category, then will use the
public function handleTopic($title){
// the code here will recognize topic and category
// and will just provide the stuff to show the right page
// in the end will redirect to that controller
return redirect()->action('DetailController#index', ['category' => $category, 'title' => $title]);
}
public function index($stuffYouNeed){
// retrieve the rest of data you need to show the page
// you already know the title and category (cause of previous controller)
// in the end return the view with data
return view ('detail.detail',['data' => $data]);
}
In your routes you'll have to add one route and edit the existing one like:
Route::get('topic/{title}', 'DetailController#handleTopic')->name('handleTopic');
Route::get('topic/{category}/{title}', 'DetailController#index')->name('showTopic');
It's not tested cause atm i don't have a laravel env set up in local. But i think it should work. Let me know
Edit: I forgot to explain why you see the error
Action App\Http\Controllers/topic/{category}/{title} not defined.
You are using the redirect incorrectly
Route::get('/topic/{title}',function(){
return redirect()->action('/topic/{category}/{title}','DetailController#index');
});
You can only provide an action controller, not a route. And the destination route have to exist. So the right use is:
return redirect()->action('Controller#action');
Anyway, you should split logic from routes. You have controllers for that... even for really short blocks of code. It will keep everything in order and clear. In your route file you should have ONLY routes.
Solved, I found my way after modified. and this is the code:
Route::get('/topik/{title}',function($title){
$article = DB::table('t_artikel')
->join('t_section', 't_section.id_section', '=', 't_artikel.id_section')
->where('publish', '=', 'Y')
->where('parent_id', '=', 0)
->where('tgl_pub', '<=', date('Y-m-d H:i:s'))
->where('t_artikel.urltitle', '=', $title)
->select('t_artikel.*', 't_section.urltitle as urltitlesec', 't_artikel.urltitle as urlartikel')
->first();
$kategori = $article->urltitlesec;
return redirect("/topik/{$kategori}/{$title}");
});
If you want to redirect to a URI, then don’t use redirect()->action(); use redirect()->to() instead:
Route::get('/topic/{title}', function ($title) {
$category = ''; // Get category some how
return redirect()->to("/topic/{$category}/{$title}");
});
I've got a Controller (UsersController) with a function :
public function project_donated($project_id)
{
return Donation
::groupBy('donations.project_id')
->selectRaw('sum(donations.amount) as sum, donations.project_id, projects.*')
->where('donations.project_id', $project_id)
->join('projects', 'donations.project_id', '=', 'projects.id')
->get()
->toArray();
}
This function works fine.
I return this function into the same UserController to my view with :
$project_donated = $this->project_donated($project_id);
return view('users.show', compact('user', 'countries', 'donation_user' , 'user_donated', 'project_donated'));
But, I cannot defind $project_id into my controller, I need to specify it into my view, so into my view, I had this line :
<?php
$project_id = $ngo->project->ngo->id;
$project_donated = UsersController::project_donated($project_id);
?>
But I've got an error : Class 'UsersController' not found.
I'm not sure I'm doing this the right way.
You can call the controllers from anywhere in the application. But it is not the general practice. Go through some general practices.
App::make('controller name')->public_methodName(params);
Similar question has been raised in the following thread.
How to call a controller from within a view - Laravel
Hope this helps. :)
I have some problem here.
I wanna view all data sort by "kelompok".
*kelompok means group
This is the code :
Controller
public function pengelompokan()
{
$view = DB::table('tb_siswa')->where('id', $kelompok)->get();
return view('pengelompokan')
->with('view', $view);
}
Route
Route::get('kelompok', 'belajarController#kelompok');
You can use the groupBy collection method:
$view = DB::table('tb_siswa')
->where('id', $kelompok)
->get()
->groupBy('kelompok');
Edit
Based on your comments, you could do this:
Route::get('kelompok/{groupId}', 'belajarController#kelompok');
public function pengelompokan($kelompok)
{
$view = DB::table('tb_siswa')
->where('id', $kelompok)
->get()
->groupBy('kelompok');
return view('pengelompokan', compact('view'));
}
Following is the code to resolve this
public function pengelompokan()
{
$view = DB::table('tb_siswa')->where('id', $kelompok)
->groupBy('kelompok')->get();
return view('pengelompokan')->with('view');
}
You can access groupBy data using a variable $view on blade as well.
I am using Routes but you can apply it on your Controller#show
Route::get('tutorial/{id}', function($id){
$tutorial = Tutorial::findOrFail($id);
return view('tutorial.show')->with('tutorial', $tutorial);})->name('show-tutorial');
and Also Check on your show.blade.php
I am calling getting_started route after successfully login :
protected $redirectTo = '/getting_started';
Here is my getting_started route code :
Route::get('/getting_started','UserController#getting_started');
And controller code :
public function getting_started()
{
$id= Auth::id();
$user = DB::table('user_profiles')->where('user_id', '=', $id)->first();
if($user->dashboard_access == 0)
{
DB::table('user_profiles')
->where('user_id', $id)
->update(['dashboard_access' => 1]);
return view('user.getting_started');
}
return view('user.dashboard');
}
It works perfectly and show in url :
http://localhost:8080/getting_started
Now I actually want that if user.dashboard view is call it show in url like :
http://localhost:8080/dashboard`
And on getting_started view show :
http://localhost:8080/getting_started
It is possible to call dashboard route instead of :
return view('user.dashboard');
My dashobard route is :
Route::get('/dashboard',['middleware' => 'auth', function () {
return view('user.dashboard');
}]);
What I understand it is that you are looking for is this function
return redirect()->route('dashboard');
It's my understanding of your question which can be wrong. Maybe you are asking something else.
That called Redirection and especially you want to Returning A Redirect To A Named Route, you route called user.dashboard so you could redirect to it using redirect()->route(route_name) :
return redirect()->route('user.dashboard');
Hope this helps.