[英]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.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
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
看到这个
\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.