如何使用GET参数制作正确的分页?

huangapple go评论68阅读模式
英文:

How to make correct pagination with GET parameters?

问题

我正在使用Laravel进行数据分页输出。一切都运行良好。我有自己的分页模板。它写成这样:

{{ $houses->links('catalogs.paginate') }}

然而,当我在页面上使用一些参数仍然搜索数据时,GET参数不会进入分页。

我尝试这样写:

{{ $houses->links('catalogs.paginate')->withQueryString() }}

或者

控制器
...

public function Category($cat, Request $request) {
    $houses = House::with('areas')->where('format_house', '=', $format_house)->paginate(4)->appends(request()->query());
    return view('category_catalog', compact('title', 'houses'));
}

模板

{{ $houses->links('catalogs.paginate') }}

没有什么帮助。应该如何正确操作?

英文:

I am doing data output with pagination on laravel. Everything works great. I have my own pagination template. It's written like this:

{{ $houses->links('catalogs.paginate') }}

However, when I still search for data on the page by some parameters, the GET parameters do not get into pagination.

I tried to write like this:

{{ $houses->links('catalogs.paginate')->withQueryString() }}

Or

controller
...

public function Category($cat, Request $request) {
$houses = House::with('areas')->where('format_house', '=', $format_house)->paginate(4)->appends(request()->query());
return view('category_catalog', compact('title', 'houses'));
}

Maket

{{ $houses->links('catalogs.paginate') }}

Nothing helps. How to do it right?

答案1

得分: 1

你已经很接近,你调用方法的排列顺序不正确,你必须最后调用 links 方法。

$houses->paginate(15)->withQueryString()->links('catalogs.paginate')

或者

$houses->paginate(15)->appends(request()->query())->links('catalogs.paginate')
英文:

You're already close, the arrangement of the methods you're calling is incorrect, you must call the links method last.

$houses->paginate(15)->withQueryString()->links('catalogs.paginate')

OR

$houses->paginate(15)->appends(request()->query())->links('catalogs.paginate')

答案2

得分: 1

请看以下翻译:

Try this

{{ $houses->appends($_GET)->links('catalogs.paginate') }}

OR

{{ $houses->appends(request()->query())->links('catalogs.paginate') }}

英文:

Try this

{{ $houses->appends($_GET)->links('catalogs.paginate') }}

OR

{{ $houses->appends(request()->query())->links('catalogs.paginate') }}

huangapple
  • 本文由 发表于 2023年3月7日 14:54:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定