简体   繁体   English

Laravel如何连接表和合并列,然后在Eloquent上设置“ LIKE”查询?

[英]Laravel How to join table and merge columns, then set a 'LIKE' query on Eloquent?

ENV: ENV:

Laravel 5.7.28

Database mysql 数据库mysql

CentOS 7.2

I have 3 table like as below, I need join this 3 table and merge columns ( customer.first_name,customer.last_name,customer.address,job.name,job.address,job.date ) to set 'like' query. 我有3个表,如下所示,我需要加入这3个表并合customer.first_name,customer.last_name,customer.address,job.name,job.address,job.datecustomer.first_name,customer.last_name,customer.address,job.name,job.address,job.date )以设置“喜欢”查询。

For example, After coulms merge 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 is 'TOMSMITHCecilia ChapmanABC.LtdIris Watson2019-01-10', so 例如,在库尔姆合并了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 result


customer.first_name = TOM

customer.last_name = SMITH

customer.address = Cecilia Chapman

job.name = ABC.Ltd

job.address = Iris Watson

job.date = 2019-01-10

  1. id table (relation belongs To table customer and job) id表(关系属于表客户和工作)
    • id ID
    • customer_id 顾客ID
    • job_id job_id
  2. customer table 客户表
    • id ID
    • first_name 名字
    • last_name
    • address 地址
  3. job id 工作编号
    • id ID
    • name 名称
    • address 地址
    • job_date job_date

See this 看到这个

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

If you have multiple where condition then you may use where function with orWhere like this: 如果您有多个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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM