繁体   English   中英

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

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

ENV:

Laravel 5.7.28

数据库mysql

CentOS 7.2

我有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 )以设置“喜欢”查询。

例如,在库尔姆合并了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表(关系属于表客户和工作)
    • ID
    • 顾客ID
    • job_id
  2. 客户表
    • ID
    • 名字
    • 地址
  3. 工作编号
    • ID
    • 名称
    • 地址
    • job_date

看到这个

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

如果您有多个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