Tismon Varghese
Tismon Varghese

Reputation: 381

Concatenate two columns in eloquent where clause in laravel

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();

Upvotes: 3

Views: 6641

Answers (2)

MyLibary
MyLibary

Reputation: 1771

Try the following code:

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

The first argument should be the gluing piece.

Upvotes: 3

Devon Bessemer
Devon Bessemer

Reputation: 35337

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

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

Upvotes: 6

Related Questions