I intend to paginate a laravel eloquent model using a where clause. I expect the page links to show and be clickable leading to the clicked page. This is the default controller in which I paginate without a where clause.
public function index(Request $request){
$users = User::paginate(12)->onEachSide(1);
return view('/**',compact('users'));
}
I am able to paginate through till the last page. This is the route (I have placed the name is asterisks for confidentiality.:
Route::get('/**', 'SearchController@index');
This functions better, however I try to limit the rows with a where clause:
public function search(Request $request){
$location = $request->location;
$talent = $request ->talent;
$users = User::where('talent',$talent)->where('location',$location)->paginate(12);
return view('/searchResult',compact('users');
}
This is the route:
Route::get('/**', 'SearchController@search');
However, in the second case, the pagination result returns blank when I try to navigate. I am not sure why this is happening. On the front view, I have:
{{$users->links()}}
$location
and$talent
come from the query string, you need to use{{$users->withQueryString()->links()}}
in order for those query parameters to be passed to the next page – lufc{{ $users->appends(Request::except('page'))->links() }}
for Laravel 5. – lufc