Issue
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
Solution
If you want to apply filters to the next page you should add them to your paginator like this:
$product = Product::where('naam', 'LIKE', '%' . $q . '%')
->orWhere('beschrijving', 'LIKE', '%' . $q . '%')
->paginate(6);
$product->appends(['search' => $q]);
And change your route from post to get:
Route::get('search', 'IndexController@search');
Answered By - Andrej Ludinovskov
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.