dongliaojing0554 2017-06-01 16:17
浏览 36
已采纳

Laravel Pagination with Get请求

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.

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

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() }}

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?

Thanks.

  • 写回答

1条回答 默认 最新

  • douchui4459 2017-06-01 16:29
    关注

    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'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀