雄辩的WHERE LIKE子句具有多列 [英] Eloquent WHERE LIKE clause with multiple columns

查看:56
本文介绍了雄辩的WHERE LIKE子句具有多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个搜索栏,可以按名字,姓氏或两者来搜索客户.因此,例如,Mike Hizer将与MikeHizerzerMike Hizer等匹配.这是我想出的:

I'm implementing a search bar that can search customers by first name, last name, or both. So, for example, Mike Hizer would be matched by Mike, Hizer, zer, Mike Hizer, etc. Here's what I came up with:

Customer::where(
    DB::raw('concat(first_name," ",last_name)'), 'like', "%{$request->search}%"
)->get()

有效.但是,是否有一种更雄辩的方法来组合这两个列(first_namelast_name)而无需求助于DB Facade?会是

It works. But is there a more Eloquent approach to combine these two columns (first_name and last_name) without resorting to the DB facade? Would something like

->where(['first_name','last_name'], 'like', "%{$request->search}%")

有可能实现吗?

推荐答案

如果您不想使用DB外观,则可以使用whereRaw方法:

If you don't want to use the DB facade you could use the whereRaw method:

Customer::whereRaw('concat(first_name," ",last_name) like ?', "%{$request->search}%")->get();

希望这会有所帮助!

这篇关于雄辩的WHERE LIKE子句具有多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆