搜索结果的分页 laravel 5.3

2022-08-30 18:04:32

分页搜索结果

我刚刚开始使用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', '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:

根据请求获取错误。

路线:

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

我希望我的问题很清楚,格式正确。提前谢谢你(对不起我的英语不好)


答:

我最终将这篇文章的答案与这篇文章的一些帮助结合起来

我为初始搜索使用了一个post函数,并为以下页面使用了一个get函数。这是可能的,因为我现在正在将搜索提供给URL。


编辑:

  • 添加了初始错误。
  • 添加了错误Route::get
  • 添加的答案

答案 1

如果要将筛选器应用于下一页,则应将它们添加到分页器中,如下所示:

$product = Product::where('naam', 'LIKE', '%' . $q . '%')
        ->orWhere('beschrijving', 'LIKE', '%' . $q . '%')
        ->paginate(6);
$product->appends(['search' => $q]);

并将您的路线从邮寄更改为获得:

Route::get('search', 'IndexController@search');

答案 2

快速查看 (Lavarel 5.7)

$product->appends(Request::all())->links();

推荐