亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 如何連接表和合并列,然后在 Eloquent 上設置“LIKE”查詢?

Laravel 如何連接表和合并列,然后在 Eloquent 上設置“LIKE”查詢?

PHP
冉冉說 2021-11-05 14:54:11
環境:Laravel 5.7.28數據庫 mysqlCentOS 7.2我有 3 個表,如下所示,我需要加入這 3 個表并合并列 ( customer.first_name,customer.last_name,customer.address,job.name,job.address,job.date) 來設置“喜歡”查詢。例如,在 coulms 合并customer.first_name,customer.last_name,customer.address,job.name,job.address,job.date customer.first_name + customer.last_name + customer.address + job.name + job.address +job.date 之后是 'TOMSMITHCecilia ChapmanABC.LtdIris Watson2019-01-10',所以 when $text = 'MS';set  'like' '%'.$text.'%' will return below resultcustomer.first_name = TOMcustomer.last_name = SMITHcustomer.address = Cecilia Chapmanjob.name = ABC.Ltdjob.address = Iris Watsonjob.date = 2019-01-10id 表(關系屬于表客戶和工作)ID顧客ID作業編號客戶表ID名姓地址工作編號ID姓名地址工作日期
查看完整描述

2 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

看到這個


\DB::table('id')->join('customer','customer.customer_id','id.customer_id')

->join('job','job.id','id.job_id')

->where('customer.first_name', 'LIKE', '%' . $text . '%')

->orWhere('customer.last_name', 'LIKE', '%' . $text . '%')

->orWhere('customer.address', 'LIKE', '%' . $text . '%')

->orWhere('job.name', 'LIKE', '%' . $text . '%')

->orWhere('job.address', 'LIKE', '%' . $text . '%')

->get();


查看完整回答
反對 回復 2021-11-05
?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

如果您有多個 where 條件,那么您可以像這樣使用 where 函數和 orWhere:

\DB::table('id')->join('customer','customer.customer_id','id.customer_id')->join('job','job.id','id.job_id')->where(function ($query) use($text) {                $query->where('customer.first_name', 'LIKE', '%' . $text .'%')
                      ->orWhere('customer.last_name', 'LIKE', '%' . $text . '%')
                      ->orWhere('customer.address', 'LIKE', '%' . $text . '%')
                      ->orWhere('job.name', 'LIKE', '%' . $text . '%')
                      ->orWhere('job.address', 'LIKE', '%' . $text . '%')
            })->get();


查看完整回答
反對 回復 2021-11-05
  • 2 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號