最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Laravel Eloquent ORM 多條件查詢的例子

 更新時(shí)間:2019年10月10日 09:28:28   作者:菜鳥沒翅膀  
今天小編就為大家分享一篇Laravel Eloquent ORM 多條件查詢的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

一、需求:

在數(shù)據(jù)搜索時(shí)最常見的就是調(diào)用同一個(gè)方法查詢,而查詢的字段卻可能是其中一個(gè)或其中的幾個(gè)字段一起組合查詢,例如:對列表的搜索,基本上都是幾個(gè)字段隨意組合搜索。那么在model里就需要判斷有那個(gè)字段組合,怎么組合。

網(wǎng)上找了很久,Laravel群里也問了幾個(gè),都說沒有寫過,于是自己寫個(gè)吧。話不多說,見代碼:

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); 

調(diào)用的時(shí)候,controller里只需要接收這些字段,無論它是否有值,直接加入到$param數(shù)組中查詢就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 多條件查詢的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

日喀则市| 平定县| 清镇市| 洛川县| 江门市| 南京市| 龙川县| 临潭县| 陆丰市| 萨嘎县| 罗平县| 彭阳县| 娱乐| 遂宁市| 双柏县| 贵德县| 松阳县| 嫩江县| 阿鲁科尔沁旗| 江陵县| 张家港市| 信丰县| 额尔古纳市| 日照市| 天祝| 青海省| 康乐县| 潜山县| 富源县| 绍兴市| 湘西| 武冈市| 务川| 靖宇县| 兰考县| 仪征市| 新建县| 炎陵县| 兴仁县| 五家渠市| 芜湖市|