无为清净楼资源网 Design By www.qnjia.com
一、需求:
在数据搜索时最常见的就是调用同一个方法查询,而查询的字段却可能是其中一个或其中的几个字段一起组合查询,例如:对列表的搜索,基本上都是几个字段随意组合搜索。那么在model里就需要判断有那个字段组合,怎么组合。
网上找了很久,Laravel群里也问了几个,都说没有写过,于是自己写个吧。话不多说,见代码:
function findByParam($param = array()) { $select = new Customer(); if (isset($param['name']) && '' != $param['name']) { $select = $select->where('customer.name', '=', $param['name']); } if (isset($param['phone']) && '' != $param['phone']) { $select = $select->where('customer.phone', '=', $param['phone']); } if (isset($param['email']) && '' != $param['email']) { $select = $select->where('customer.email', '=', $param['email']); } if (isset($param['tel']) && '' != $param['tel']) { $select = $select->where('customer.tel', '=', $param['tel']); } if (isset($param['qq']) && '' != $param['qq']) { $select = $select->where('customer.qq', '=', $param['qq']); } if (isset($param['IDCard']) && '' != $param['IDCard']) { $select = $select->where('customer.IDCard', '=', $param['IDCard']); } $customers = $select->leftJoin("member", function ($join) { $join->on("customer.memberID", "=", "member.id"); }) ->get(array( 'customer.id', 'customer.name', 'customer.sex', 'customer.tel', 'customer.phone', 'customer.address', 'customer.email', 'customer.qq', 'customer.headPic', 'customer.birthday', 'customer.IDCard', 'customer.enable', 'customer.memberID', 'customer.IDCard', 'customer.info', 'member.name as mname', 'member.discount' )); return json_encode($customers);
调用的时候,controller里只需要接收这些字段,无论它是否有值,直接加入到$param数组中查询就OK,例如:
function anyFindbyparam() { $name = Input::get('name'); $tel = Input::get('tel'); $phone = Input::get('phone'); $email = Input::get('email'); $qq = Input::get('qq'); $IDCard = Input::get('IDCard'); $customer = new Customer(); $customers = $customer->findByParam(array( 'name' => $name, 'tel' => $tel, 'phone' => $phone, 'email' => $email, 'qq' => $qq, 'IDCard' => $IDCard )); return $customers; }
以上这篇Laravel Eloquent ORM 多条件查询的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 范莉丝.1979-送郎一朵牵牛花(LP版)【大联机构】【WAV+CUE】
- 刘宇.2024-宇宙诗人【哔哟哔哟文化】【FLAC分轨】
- 《全景声白噪音》沉浸式大自然助眠 带你听自然的声音[mp3]
- 纯音入心系列纯音乐《古筝精选 十指弹一赏心曲一心奏出世人心》1CD[MP3][582MB]
- 纯音入心系列纯音乐《古筝与现代音乐的交织》1CD[MP3][96MB]
- 王俪婷.2011-ROMANCE【华研国际】【WAV+CUE】
- 狮子合唱团.2016-LION(引进版)【华纳】【WAV+CUE】
- 范文芳.1996-FNNTASY【HYPE】【WAV+CUE】
- 纯音入心系列纯音乐《韵味古筝曲》1CD[MP3][146.2MB]
- 纯音入心系列纯音乐《古筝系列-传统乐曲》1CD[MP3][974.7MB]
- 纯音入心系列纯音乐《古筝系列-弦凝指咽声停处》1CD[MP3][656.5MB]
- 群星.1994-大烂片2辑【派森】【WAV+CUE】
- 吴倩莲.1997-望爱【EMI百代】【WAV+CUE】
- 杨千嬅.2002-万紫千红演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郭采洁.2015-Begin.Again爱造飞鸡【华纳】【FLAC分轨】