Pagination search results
I have just started with Laravel and I am trying to make a search function with proper pagination. The function works for page one but on page two it doesn't. I think it's not giving the results to the next page but I can't seem to find an answer.
this is my search function inside IndexController:
public function search()
{
$q = Input::get('search');
# going to next page is not working yet
$product = Product::where('naam', 'LIKE', '%' . $q . '%')
->orWhere('beschrijving', 'LIKE', '%' . $q . '%')
->paginate(6);
return view('pages.index', compact('product'));
}
this is my route:
Route::post('search{page?}', 'IndexController@search');
this is the URL of page two:
/search?page=2
this is how I show my pagination:
{{ $product->appends(Request::get('page'))->links()}}
the error:
MethodNotAllowedHttpException in RouteCollection.php line 218:
Get error on request.
Route:
Route::get('search/{page?}', 'IndexController@search');
Error:
MethodNotAllowedHttpException in RouteCollection.php line 218:
in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 780
at Router->findRoute(object(Request)) in Router.php line 610
at Router->dispatchToRoute(object(Request)) in Router.php line 596
at Router->dispatch(object(Request)) in Kernel.php line 267
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
I hope my question is clear and in the right format. Thank you in advance (sorry for my bad English)
Answer:
I ended up using the answer of this post in combination with some help of this post
I used a post function for the initial search and a get function for the following pages. This was possible because I'm now giving my search to the URL.
EDIT:
- added the initial error.
- added the
Route::get
error - added answer
and in your view page
make your route to get method
this will be done, thanks,
in my case, i've laravel 5.7 installed.
and my view files codes are
for per_page select dropdown and search area
and my pagination generator code
For pagination, you should create a simple form:
Pagination methods are here:
If you want to apply filters to the next page you should add them to your paginator like this:
And change your route from post to get:
If you are using search form with GET method then use something like these to preserve pagination withing search results.
I assume you want to change pages with urls like this
search/1
,search/2
? First of all your route should be probablyRoute::post('search/{page?}')
.I'm not sure if only this change will work, but if it does not, you have to resolve page like this