Laravel實現(xiàn)ORM帶條件搜索分頁
更新時間:2019年10月24日 10:13:06 作者:一夜長風(fēng)
今天小編就為大家分享一篇Laravel實現(xiàn)ORM帶條件搜索分頁,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
Laravel條件搜索一般使用where方法,如下:
查詢構(gòu)造器:
$users = DB::table('users')->where('votes', '=', 100)->get();
或者ORM:
$users = User::where('votes', '=', 100)->all();
當(dāng)有多個條件時,可以多次調(diào)用where方法:
$articles = Article::where('id','>','10')->where('is_auth','=','1')->where('id','=','14')->paginate(3)
所以,如果需要進行多條件搜索分頁,我們可以這么寫:
public function index(Request $request)
{
//分類表數(shù)據(jù)
$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() !!}
效果如圖:

以上這篇Laravel實現(xiàn)ORM帶條件搜索分頁就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Laravel5.5 手動分頁和自定義分頁樣式的簡單實現(xiàn)
- PHP框架Laravel插件Pagination實現(xiàn)自定義分頁
- Laravel手動分頁實現(xiàn)方法詳解
- Laravel+jQuery實現(xiàn)AJAX分頁效果
- Laravel框架執(zhí)行原生SQL語句及使用paginate分頁的方法
- laravel實現(xiàn)分頁樣式替換示例代碼(增加首、尾頁)
- laravel自定義分頁效果
- Laravel框架搜索分頁功能示例
- laravel自定義分頁的實現(xiàn)案例offset()和limit()
- Laravel實現(xiàn)搜索的時候分頁并攜帶參數(shù)
- 在Laravel中實現(xiàn)使用AJAX動態(tài)刷新部分頁面
- Laravel5.1 框架分頁展示實現(xiàn)方法實例分析
相關(guān)文章
PHP獲取短鏈接跳轉(zhuǎn)后的真實地址和響應(yīng)頭信息的方法
這篇文章主要介紹了PHP獲取短鏈接跳轉(zhuǎn)后的真實地址和響應(yīng)頭信息的方法,本文使用get_headers函數(shù)實現(xiàn),需要的朋友可以參考下2014-07-07
php中重定向網(wǎng)頁跳轉(zhuǎn)方法總結(jié)案例教程
這篇文章主要介紹了php中重定向網(wǎng)頁跳轉(zhuǎn)方法總結(jié)案例教程,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
PHP ajax+jQuery 實現(xiàn)批量刪除功能實例代碼小結(jié)
這篇文章主要介紹了PHP ajax+jQuery 實現(xiàn)批量刪除功能實例代碼小結(jié),代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-12-12

