在laravel中雄辩的where子句中连接两列

时间:2018-07-31 17:11:37

标签: php mysql laravel eloquent lumen

在我的数据库中,country_codephone_number是两个不同的字段。用户正在输入一个包含国家代码和电话号码的字符串来登录。为了验证这一点,我必须将雄辩的where子句中的列country_codephone_number连接起来。

有人可以告诉我该怎么做吗?我在下面提供我的查询。

$user  = self::where('phone_number', '=', $username)->get()->first();

2 个答案:

答案 0 :(得分:3)

您可以将whereRaw与原始SQL表达式一起使用,并将参数与第二个参数绑定。

whereRaw("CONCAT(`country_code`, `phone_number`) = ?", [$username]);

答案 1 :(得分:2)

尝试以下代码:

$user = self::whereRaw("CONCAT_WS(' ',`country_code`, `phone_number`) = ? ",  [$username])->get()->first();

第一个参数应该是粘合片。

相关问题