loading
Office 01
JL. RAYA SAWANGA, MAMPANG
Pancoran Mas, Depok
+628111026606
Mon - Fri 9:00am - 5:00 pm
[email protected]
24 X 7 online support

How to paginate search results in laravel?

How to paginate search results in laravel?
How to paginate search results in laravel?
How to paginate search results in laravel?

There is 2 ways to paginating your search results in laravel.

  1. From blade file
  2. From controller file

 

1. From blade file

in your view where you display pagination...

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

appends keeps the query string value except "page".

Note: don't forget to change your route from post to get.

 

2. From your controller file

public function search(Request $request) {

  $search = request('search');

  $posts = Post::where('title', 'like', "%{$search}%")
         ->orWhere('description', 'like', "%{$search}%")
         ->where('publish', '=', 1)
         ->paginate(10);
  $posts->appends(['search' => $search]);

  if(count($posts) > 0) {
    return view('front.search.index', compact('posts', 'search'));
  } else {
    return view ('front.search.index', compact('posts','search'));
  }

}
PHP

we have included append to our controller in order to get results in pages while they passed to our url

$posts->appends(['search' => $search]);
PHP

 

Simple isn't it?

Be the first to leave a comment.

Post a Comment

back to top