在laravel中的eloquent where子句中连接两列

In my database, country_code and phone_number are two different fields. 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.

Can someone tell me how to do this ? I am giving my query below.

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

You can use whereRaw with a raw SQL expression and bind the parameters with the second argument.

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.