Laravel分页与获取请求 [英] Laravel Pagination with Get request

查看:58
本文介绍了Laravel分页与获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照Laravel文档中的说明使用appends([])进行分页,但是这些参数的持久性存在一些麻烦.

I've followed the instructions on the Laravel documentation for pagination with appends([]) however I'm having a little trouble with the persistence of these parameters.

例如,我将home?category=Cars&make=Tesla传递给我的视图.与他们分页获取请求的最佳方法是什么?

Say for example, I pass home?category=Cars&make=Tesla to my view. What is the best way to paginate with them Get requests?

现在,我已经将类别作为参数传递给视图(其中类别是我已经用request('category');抓住了findOrFail的模型)

Right now I've passed the category as a parameter to the view as (where category is the model i've grabbed findOrFail with the request('category');)

$category_name = $category_model->name;

然后在我看来,就像这样:

And then in my view it's like so:

{{ $vehicles->appends(['category' => $category_name])->links() }}

但是当我在分页中浏览页面时,此$ category_name值似乎并不持久.建议实现我想要的方法是什么?

But when I go between pages in the pagination, this $category_name value doesn't seem to persist. Whats the recommended way to achieve what I want?

谢谢.

推荐答案

在对结果进行分页时,可以在控制器中附加查询字符串.我不确定这是否是您唯一的问题,甚至不确定是否将查询字符串作为条件.因此,这里有一个示例向您展示如何同时进行.这应该使您知道如何执行此操作.在此示例中,我只是假设了列名.

You can append the query string in your controller when you paginate the result. I'm not sure if that was your only question or even regarding applying the query string as a condition. So here is a sample showing you how to do both. This should give you an idea of how to do it. I just assumed the column names in this example.

$category = request('category');
$make = request('make');

$vehicles = Vehicle::when($category, function ($query) use ($category) {
        return $query->where('category', $category);
    })
    ->when($make, function ($query) use ($make) {
        return $query->where('make', $make);
    })
    ->paginate(10);

$vehicles->appends(request()->query());

return view('someview', compact('vehicles'));

这篇关于Laravel分页与获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆