佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Laravel Eloquent ORM 多條件查詢的例子

Laravel Eloquent ORM 多條件查詢的例子

熱門(mén)標(biāo)簽:Linux服務(wù)器 服務(wù)器配置 阿里云 Mysql連接數(shù)設(shè)置 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 銀行業(yè)務(wù) 團(tuán)購(gòu)網(wǎng)站 電子圍欄

一、需求:

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

網(wǎng)上找了很久,Laravel群里也問(wèn)了幾個(gè),都說(shuō)沒(méi)有寫(xiě)過(guò),于是自己寫(xiě)個(gè)吧。話不多說(shuō),見(jiàn)代碼:

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里只需要接收這些字段,無(wú)論它是否有值,直接加入到$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 多條件查詢的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Laravel5.7 Eloquent ORM快速入門(mén)詳解
  • Laravel 5框架學(xué)習(xí)之Eloquent (laravel 的ORM)
  • Laravel Eloquent ORM 實(shí)現(xiàn)查詢表中指定的字段
  • laravel 解決Eloquent ORM的save方法無(wú)法插入數(shù)據(jù)的問(wèn)題
  • Laravel框架Eloquent ORM新增數(shù)據(jù)、自定義時(shí)間戳及批量賦值用法詳解
  • laravel框架數(shù)據(jù)庫(kù)操作、查詢構(gòu)建器、Eloquent ORM操作實(shí)例分析
  • Laravel框架Eloquent ORM刪除數(shù)據(jù)操作示例
  • laravel 數(shù)據(jù)遷移與 Eloquent ORM的實(shí)現(xiàn)方法
  • Laravel框架Eloquent ORM簡(jiǎn)介、模型建立及查詢數(shù)據(jù)操作詳解
  • Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
  • laravel5.6 框架操作數(shù)據(jù) Eloquent ORM用法示例

標(biāo)簽:大理 江蘇 萍鄉(xiāng) 棗莊 衢州 廣元 蚌埠 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel Eloquent ORM 多條件查詢的例子》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    怀化市| 宁陕县| 枞阳县| 陆川县| 肥城市| 扎鲁特旗| 囊谦县| 衡山县| 南和县| 惠州市| 鹿泉市| 拉萨市| 汉沽区| 洱源县| 福泉市| 静乐县| 峡江县| 宜川县| 和林格尔县| 寻甸| 鄂温| 甘孜| 沙雅县| 华池县| 柳州市| 龙海市| 锡林郭勒盟| 罗田县| 岱山县| 沂源县| 南木林县| 永宁县| 宝丰县| 衡水市| 天等县| 南京市| 始兴县| 沧州市| 卓资县| 古田县| 桐柏县|