Pagination for search results laravel 5.3(搜索结果的分页 laravel 5.3)
问题描述
我刚刚开始使用 Laravel,我正在尝试使用适当的分页来制作搜索功能.该功能适用于第一页,但在第二页上不起作用.我认为它没有将结果提供给下一页,但我似乎找不到答案.
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.
这是我在 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'));
}
这是我的路线:
Route::post('search{page?}', 'IndexController@search');
这是第二页的网址:
/search?page=2
这就是我显示分页的方式:
{{ $product->appends(Request::get('page'))->links()}}
错误:
MethodNotAllowedHttpException in RouteCollection.php line 218:
<小时>
根据请求获取错误.
Get error on request.
路线:
Route::get('search/{page?}', 'IndexController@search');
错误:
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->IlluminateFoundationHttp{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->IlluminateRouting{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)
答案:
我最终结合了这篇文章的答案并结合了 这个帖子
I ended up using the answer of this post in combination with some help of this post
我在最初的搜索中使用了 post 函数,在接下来的页面中使用了 get 函数.这是可能的,因为我现在正在搜索 URL.
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.
- 添加了初始错误.
- 添加了
Route::get
错误 - 添加答案
推荐答案
如果您想将过滤器应用到下一页,您应该将它们添加到您的分页器中,如下所示:
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');
这篇关于搜索结果的分页 laravel 5.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:搜索结果的分页 laravel 5.3
基础教程推荐
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01