There is 2 ways to paginating your search results in laravel.
- From blade file
- 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?
- Last updated 4 years ago
Be the first to leave a comment.
You must login to leave a comment