Laravel5怎么实现搜索分页能保留搜索条件
作者:
秒速五厘米
Laravel条件搜索一般使用where方法,如下:
查询构造器:
$users = DB::table('users')->where('votes', '=', 100)->get();
或者ORM:
$users = User::where('votes', '=', 100)->all();
当有多个条件时,可以多次调用where方法:
$articles = Article::where('id','>','10') ->where('is_auth','=','1') ->where('id','=','14') ->paginate(3)
所以,如果需要进行多条件搜索分页,我们可以这么写:
public function index(Request $request) { //分类表数据 $sorts = Sortart::all(); //文章表实例化 $article = new Article; //搜索条件判断 $where = $article; if($request->search_sid){ $where = $where->where('sort_id','=',$request->search_sid); } if($request->search_title){ $where = $where->where('title','like','%'.$request->search_title.'%'); } //分页搜索 $articles = $where->paginate(3); //搜索条件保持 $articles->sid = $request->search_sid; $articles->title = $request->search_title; //$articles = Article::where('id','>','10')->where('is_auth','=','1')->where('id','=','14')->paginate(3); //dd($articles); return view('admin.articles.index',compact('articles','sorts')); }
在页面中,使用url保持分页条件的方法:
{!! $articles->appends(['search_sid'=>$articles->sid, 'search_title'=>$articles->title])->render() !!}
效果如图:
原文:https://blog.csdn.net/wlzx120/article/details/77509021