沃梦达 / 编程问答 / php问题 / 正文

搜索结果的分页 laravel 5.3

Pagination for search results laravel 5.3(搜索结果的分页 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

基础教程推荐