为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 141 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信