【发布时间】: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() }}。 -
好的,我会试试这个,一旦我进入我的环境,就会回复你。不过,我在文档中找不到太多内容。