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 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起