Laravel 分页链接不包括其他 GET 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17159273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:31:04  来源:igfitidea点击:

Laravel Pagination links not including other GET parameters

paginationlaravellaravel-4eloquentblade

提问by Nyxynyx

I am using Eloquent together with Laravel 4's Pagination class.

我将 Eloquent 与 Laravel 4 的 Pagination 类一起使用。

Problem:When there are some GET parameters in the URL, eg: http://site.com/users?gender=female&body=hot, the pagination links produced only contain the pageparameter and nothing else.

问题:当 URL 中有一些 GET 参数时,例如: http://site.com/users?gender=female&body=hot,产生的分页链接只包含该page参数而没有其他内容。

Blade Template

刀片模板

{{ $users->link() }}

There's a ->append()function for this, but when we don't know how many of the GET parameters are there, how can we use append()to include the other GET parameters in the paginated links without a whole chunk of ifcode messing up our blade template?

有一个->append()函数可以做到这一点,但是当我们不知道有多少个 GET 参数时,我们如何使用append()在分页链接中包含其他 GET 参数而不用一大块if代码弄乱我们的刀片模板?

回答by Alexandre Danault

EDIT: Connor's comment with Mehdi's answer are required to make this work. Thanks to both for their clarifications.

编辑:Connor 对 Mehdi 的回答的评论是完成这项工作所必需的。感谢他们的澄清。

->appends()can accept an array as a parameter, you could pass Input::except('page'), that should do the trick.

->appends()可以接受一个数组作为参数,你可以通过Input::except('page'),这应该可以解决问题。

Example:

例子:

return view('manage/users', [
    'users' => $users->appends(Input::except('page'))
]);

回答by Hassan Azimi

I think you should use this code in Laravel 5. Also this will work not only with parameter pagebut also with any other parameter(s):

我认为您应该在 Laravel 5 中使用此代码。此外,这不仅适用于参数page,还适用于任何其他参数:

$users->appends(request()->input())->links();

Personally, I try to avoid using Facadesas much as I can. Using global helper functions is less code and much elegant.

就个人而言,我尽量避免使用Facades。使用全局辅助函数代码更少,更优雅。

UPDATE:

更新:

Do not use InputFacade as it is deprecated in Laravel v6+

不要使用InputFacade,因为它在 Laravel v6+ 中已被弃用

回答by Mehdi Maghrooni

Be aware of the Input::all(), it will Include the previous ?page=values again and again in each page you open !
for example if you are in ?page=1and you open the next page, it will open ?page=1&page=2
So the last value page takes will be the page you see ! not the page you want to see

要注意Input::all(),这将包括以前?page=在每次打开页面连连值!
例如,如果您进入?page=1并打开下一页,它将打开?page=1&page=2
所以最后一个值页面将是您看到的页面!不是你想看的页面

Solution : use Input::except(array('page'))

解决方法:使用 Input::except(array('page'))

回答by Aleksandr Kopelevich

You could use

你可以用

->appends(request()->query())

Example in the Controller:

控制器中的示例:

$users = User::search()->order()->with('type:id,name')
    ->paginate(30)
    ->appends(request()->query());

return view('users.index', compact('users'));

Example in the View:

视图中的示例:

{{ $users->appends(request()->query())->links() }}

回答by Bald

Not append()but appends()So, right answer is:

Not append()but appends()So,正确答案是:

{!! $records->appends(Input::except('page'))->links() !!}

回答by ecairol

LARAVEL 5

拉拉维尔 5

The view must contain something like:

视图必须包含以下内容:

{!! $myItems->appends(Input::except('page'))->render() !!}

{!! $myItems->appends(Input::except('page'))->render() !!}

回答by Yevgeniy Afanasyev

Use this construction, to keep all input params but page

使用这种结构,保留所有输入参数,但页面

{!! $myItems->appends(Request::capture()->except('page'))->render() !!}

Why?

为什么?

1) you strip down everything that added to request like that

1)你删除所有添加到请求的东西

  $request->request->add(['variable' => 123]);

2) you don't need $request as input parameter for the function

2) 你不需要 $request 作为函数的输入参数

3) you are excluding "page"

3)您不包括“页面”

PS) and it works for Laravel 5.1

PS),它适用于 Laravel 5.1

回答by Siva Ganesh

Include This In Your View Page

将此包含在您的查看页面中

 $users->appends(Input::except('page'))

回答by Mostafa Asadi

for who one in laravel 5 or greater in blade:

对于 laravel 5 或更高版本中的刀片:

{{ $table->appends(['id' => $something ])->links() }}

you can get the passed item with

你可以得到传递的项目

$passed_item=$request->id;

test it with

测试它

dd($passed_item);

you must get $something value

你必须得到 $something 的价值

回答by Cengkuru Michael

Pass the page number for pagination as well. Some thing like this

也传递页码以进行分页。像这样的事情

$currentPg = Input::get('page') ? Input::get('page') : '1';
    $boards = Cache::remember('boards'.$currentPg, 60, function(){ return WhatEverModel::paginate(15); });