搜索结果的分页laravel 5.3 [英] Pagination for search results laravel 5.3

查看:81
本文介绍了搜索结果的分页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->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)

答案:

我最终结合了我将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错误
  • 添加的答案
  • added the initial error.
  • added the Route::get error
  • added answer

推荐答案

如果要将过滤器应用于下一页,则应将其添加到分页器中,如下所示:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆