Laravel Concat in Query (Where Condition)

Laravel concat in query (where condition)

Laravel does some stuff behind the scenes like adding in the tick marks for you.

Fortunately, it also offers a couple of tools to still get the job done for you...

For this type of thing, DB::raw() usually works really well. Try something like this...

$query->orWhere(DB::raw("CONCAT(`nvp`, ' ', `vpv`)"), 'LIKE', "%".$this->searchNeedle."%");

How to Use CONCAT in laravel where clause without using model

try this

$query = DB::table('mbo_party')->where(DB::raw("CONCAT(first,' ',last)"), 'LIKE', '%' . $party_name . '%')->first();

Concat first name and last name in where clause in laravel mongodb to search

Can't you do something like:

$full_name = explode(" ", $search);
$last_name = $full_name[0];
$first_name = $full_name[1];

Then:

->orWhere(function($query){
$query->where('last_name', $last_name)->where('first_name', $first_name);
});

Concat column name in laravel where condition

you can use DB::raw to use mysql functions:

->Where(function($query) use ($search){
$query->orWhere('cust_name', 'like' ,"$search%");
$query->orWhere('cust_phone', $search);
$query->orWhere('ward', $search);
$query ->orWhere(DB::raw("CONCAT(tbl_customer.ward, tbl_customer.cust_house_num)"),'like', $search);
})
->get();

Where like' clause using the concatenated value of 2 columns with eloquent

Mutators are not the way to go here, because they do not affect the query you're building, they are only used on the values stored in the model. However you can use the following condition:

$query->where(DB::raw('CONCAT_WS(" ", name, lastname)'), 'like', $search);

The MySQL function CONCAT_WS will concatenate two strings, gluing them with the string passed as the first parameter. So in this case if the first name is John and the last name is Smith, the condition will be evaluated to this "John Smith" like "%query%". The DB:raw is used to avoid the first part of the condition to be escaped which would put backticks around the concatenation statement.

How to use CONCAT function in Laravel Query

You can use DB::raw() to CONCAT. And you can use Request instead.

use Illuminate\Http\Request;

public function YourFunction(Request $data)
{
$checkLogin = User::query()
->where(DB::raw("CONCAT(Username, '#', LPAD(NameID, 4, '0'))"), $data->id)
->where('Password', $data->pass)
->first();
}

Make sure you put this at the top: use DB;



Related Topics



Leave a reply



Submit