宝哥软件园

Laravel实现对象关系映射(对象关系映射)带条件搜索分页

编辑:宝哥软件园 来源:互联网 时间:2021-08-19

Laravel条件搜索一般使用在哪里方法,如下:

查询构造器:

$ users=db :表(' users ')-其中('选票,'=',100)-get();或者ORM:

$ users=User:where('投票,'=',100)-all();当有多个条件时,可以多次调用在哪里方法:

$ articles=article : where(' id ','',' 10')-where('is_auth ','=',' 1')-where('id ','=',' 14 ')-分页(3)所以,如果需要进行多条件搜索分页,我们可以这么写:

公共函数索引(请求$request) { //分类表数据$ sorts=sorter t :3360 all();//文章表实例化$article=新文章;//搜索条件判断$ where=$ article if($ request-search _ sid){ $ where=$ where-where(' sort _ id ','=',$ request-search _ sid);} if($ request-search _ title){ $ where=$ where-where(' title ',' like ',' % ').$请求-搜索标题.'%');} //分页搜索$ articles=$ where-paginate(3);//搜索条件保持$ articles-sid=$ request-search _ sid;$ articles-title=$ request-search _ title;//$ articles=article :其中(' id ','',' 10 ')-其中(' is_auth ','=',' 1 ')-其中(' id ','=',' 14 ')-分页(3);//DD($文章);返回视图(' admin.articles.index ',compact('articles ',' sorts ');}在页面中,使用全球资源定位器(统一资源定位符)保持分页条件的方法:

{!$文章-追加(['search_sid'=$articles-sid,' search _ title '=$ articles-title])-render()!}效果如图:

以上这篇Laravel实现对象关系映射(对象关系映射)带条件搜索分页就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

更多资讯
游戏推荐
更多+