【问题标题】:Why is Laravel 5.7 pagination not showing more pages after the first page result为什么 Laravel 5.7 分页在第一页结果后没有显示更多页面
【发布时间】:2020-04-14 21:23:01
【问题描述】:

我打算使用 where 子句对 laravel eloquent 模型进行分页。我希望页面链接能够显示并且可以点击,从而导致点击页面。这是我在没有 where 子句的情况下分页的默认控制器。

 public function index(Request $request){

  $users = User::paginate(12)->onEachSide(1);           
  return view('/**',compact('users'));
}

我可以分页直到最后一页。这是路线(我已将名称放在星号以保密。:

Route::get('/**', 'SearchController@index');

这个功能更好,但是我尝试使用 where 子句限制行:

  public function search(Request $request){

$location = $request->location;
$talent  = $request ->talent;

$users = User::where('talent',$talent)->where('location',$location)->paginate(12);

return view('/searchResult',compact('users');    
  }

这是路线:

Route::get('/**', 'SearchController@search');

但是,在第二种情况下,当我尝试导航时,分页结果返回空白。我不确定为什么会这样。 在前视图中,我有:

              {{$users->links()}}

【问题讨论】:

  • 您使用什么代码来呈现视图中的分页链接?
  • {{$users->links()}}
  • 假设$location和$talent来自查询字符串,您需要使用{{$users->withQueryString()->links()}}才能将这些查询参数传递到下一页
  • 道歉只适用于 Laravel 7+。而是为 Laravel 5 执行 {{ $users->appends(Request::except('page'))->links() }}。
  • 好的,我会试试这个,一旦我进入我的环境,就会回复你。不过,我在文档中找不到太多内容。

标签: php laravel eloquent


【解决方案1】:

此代码有效,我现在可以有效地进行分页。不过,在评论中由 lufc 回答。不过,我对这个机制很好奇。

     {{ $users->appends(Request::except('page'))->links() }}

【讨论】:

  • 默认情况下,links() 创建的分页链接不附加现有的查询字符串。因此,如果您有一个带有mypage?location=USA 的页面,则创建的分页链接将显示mypage?page=2 并会切断第2 页的位置。appends() 函数将参数添加到 URL。在这种情况下,它将附加整个现有查询字符串 except() 由分页添加的任何页码(否则在第三页上你会得到 mypage?location=USA&page=2&page=3。
  • 请点赞您的答案。您能否分享一个链接,指向 laravel 文档或其他任何可用的更多详细信息的任何描述?
  • 感谢 lufc 的回答!很有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多