简体   繁体   English

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

[英]Concatenate two columns in eloquent where clause in laravel

In my database, country_code and phone_number are two different fields. 在我的数据库中, country_codephone_number是两个不同的字段。 The user is entering a single string containing country code and phone number to login. 用户正在输入一个包含国家代码和电话号码的字符串来登录。 In order to validate this, I have to concatenate the columns country_code and phone_number in the eloquent where clause. 为了验证这一点,我必须将雄辩的where子句中的country_codephone_number列连接起来。

Can someone tell me how to do this ? 有人可以告诉我该怎么做吗? I am giving my query below. 我在下面给出我的查询。

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

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

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

Try the following code: 尝试以下代码:

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

The first argument should be the gluing piece. 第一个参数应该是粘合件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM