Pagination search results

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.


this is my search function inside 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'));
}

this is my route:

Route::post('search{page?}', 'IndexController@search');

this is the URL of page two:

/search?page=2

this is how I show my pagination:

{{ $product->appends(Request::get('page'))->links()}}

the error:

MethodNotAllowedHttpException in RouteCollection.php line 218:

Get error on request.

Route:

Route::get('search/{page?}', 'IndexController@search');

Error:

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

我希望我的问题清晰明了,格式正确.提前谢谢(抱歉我的英语不好)


Answer:

最后,我把这篇文章的答案和this篇文章的一些帮助结合起来

我在最初的搜索中使用了post函数,在接下来的页面中使用了get函数.这是可能的,因为我现在正在搜索URL.


EDIT:

  • 已添加初始错误.
  • 添加了Route::get个错误
  • added answer

推荐答案

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

$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相关问答推荐

如何判断用户是否已登录Laravel

如何更改LIVE上的Filament占位符内容?

Laravel 10 - 没有 $append 属性的Eloquent 的热切加载

Laravel 数据未传递到下拉框

无法找到用户sail:passwd 文件中没有匹配的条目

查询 Laravel Select WhereIn 数组

Laravel 没有定义

具有实时和 WebSockets 的 Angular2 + Laravel

Eager加载:在具有eloquent关系的枢轴上使用`with`

查询构建器中的 Laravel 5 多重 Select ()

Laravel:如何在没有数据库的情况下对用户进行身份验证

在 Laravel Blade 中转义 vue.js 数据绑定语法?

如何卸载 Laravel?

如何在 Laravel 中实现自己的 Faker 提供程序

如何更改 Handlebars.js 的默认分隔符?

如何在 Laravel 5.2 中手动发送密码重置请求?

Grammar::parameterize() 必须是数组类型

Laravel:array_merge():参数#2不是数组错误

Laravel 集合计数结果

如何在 Laravel 5.3 中获取当前的语言环境