【问题标题】:Undefined offset: 1 during pagination Laravel未定义的偏移量:分页 Laravel 期间为 1
【发布时间】:2017-02-01 01:25:40
【问题描述】:

我已经设置了一个查询来通过搜索表单从数据库中搜索旅游,并对结果应用分页。以下是查询:

public function search(Request $request)
{
    $days = $request->days;
    $days_explode = explode('|', $days);

    $query= Tour::whereHas('country', function($r) use($request) {
            $r->where('countries.name','=', $request->country);
        })
        ->whereHas('category', function($s) use($request) {
            $s->where('categories.name','=', $request->category);
        }) 
        ->whereHas('country', function($r) use($request) {
            $r->where('countries.name','=', $request->country);
        })
        ->whereBetween('days', [$days_explode[0], $days_explode[1]])   
        ->paginate(1);
    return view('public.tour.list')->withResults($query);
}

当没有使用paginate 用以下数据填充(下拉)表单时。查询在视图中返回 4 个结果。

country:Croatia,category:Holiday, days:10|15

当我使用paginate() 在多个页面中获取结果时,起初它运行时没有错误但是当我单击其他页面时,例如 2,3。它给出了错误

Undefined offset: 1

搜索页面的url如下:

http://localhost:8000/trip/search?country=croatia&category=holiday&days=10%7C15

第 2 页的 URL 如下所示:

http://localhost:8000/trip/search?page=2

给出错误

`Undefined offset: 1`

我之前在 laravel 中使用过 paginate,但没有遇到过这样的问题。

【问题讨论】:

标签: php mysql laravel pagination laravel-5.3


【解决方案1】:

这里的问题,当你转到下一页时,你没有携带查询字符串数据,所以你需要将这些数据传递给你的视图,你可以使用以下

{{ $data->appends(Request::only('country','categorry','days'))->links() }}

这应该可以解决它

【讨论】:

  • 很高兴为您提供帮助:)
【解决方案2】:

您需要使用append() 方法将数据附加到分页链接:

{{ $data->appends([
       'country' => 'croatia', 
       'category' => 'holiday',
       'days' => '10%7C15'
   ])->links() }}

https://laravel.com/docs/5.3/pagination#displaying-pagination-results

【讨论】:

  • @ZacharyDale 是的,用这个代替{{ $data->links() }}{{ $data->render() }} 另外,将croatia 更改为具有此值的变量等。
  • 那么,您正在为键设置静态值吗?但在我的情况下,这些数据来自搜索表单的下拉列表。
猜你喜欢
  • 2021-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 2011-07-22
  • 2018-07-27
  • 2018-10-05
相关资源
最近更新 更多