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
我希望我的问题清晰明了,格式正确.提前谢谢(抱歉我的英语不好)
Answer:
最后,我把这篇文章的答案和this篇文章的一些帮助结合起来
我在最初的搜索中使用了post函数,在接下来的页面中使用了get函数.这是可能的,因为我现在正在搜索URL.
EDIT:
- 已添加初始错误.
- 添加了
Route::get
个错误 - added answer