0
votes

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()}}
1
What code are you using to render the pagination links within the view?lufc
{{$users->links()}}David Enoma
Assuming that $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 pagelufc
Apologies that only works in Laravel 7+. Instead do {{ $users->appends(Request::except('page'))->links() }} for Laravel 5.lufc
Alright I'll try this and get back to you once I'm in my environment. I couldn't find much in the docs though.David Enoma

1 Answers

0
votes

This code works and I am now able to paginate effectively. Answer by lufc in the comment though. However, I am curious about the mechanism.

     {{ $users->appends(Request::except('page'))->links() }}