猿问

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

环境:


Laravel 5.7.28


数据库 mysql


CentOS 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 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

    • 作业编号

  2. 客户表

    • ID

    • 地址

  3. 工作编号

    • ID

    • 姓名

    • 地址

    • 工作日期


冉冉说
浏览 142回答 2
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();

潇潇雨雨

如果您有多个 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();
随时随地看视频慕课网APP
我要回答