php – 搜索结果的分页laravel 5.3

前端之家收集整理的这篇文章主要介绍了php – 搜索结果的分页laravel 5.3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
分页搜索结果

我刚刚开始使用Laravel,我正在尝试使用适当的分页来创建搜索功能.该功能适用​​于第一页,但第二页则不适用.我认为它不会将结果提供给下一页,但我似乎无法找到答案.

这是我在IndexController中的搜索功能

public function search()
{
    $q = Input::get('search');

    # going to next page is not working yet
    $product = Product::where('naam','LIKE','%' . $q . '%')
        ->orWhere('beschrijving','%' . $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:

根据要求获取错误.

路线:

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

我希望我的问题清楚,格式正确.提前谢谢(抱歉我的英文不好)

回答:

我最后使用这篇文章的答案结合this帖子的一些帮助

我使用post函数进行初始搜索,并使用get函数进行后续页面.这是可能的,因为我现在正在搜索URL.

编辑:

>添加了初始错误.
>添加了Route :: get错误
>补充说明

如果要将过滤器应用于下一页,则应将它们添加到您的分页器中,如下所示:
$product = Product::where('naam','%' . $q . '%')
        ->paginate(6);
$product->appends(['search' => $q]);

并改变你的路线从post到get:

Route::get('search','IndexController@search');
原文链接:https://www.f2er.com/laravel/138338.html

猜你在找的Laravel相关文章