how to use raw sql with laravel eloquent builder

2070 views php
7

I need to get name and id from my user table, name is in two column as first_name and last_name. This is what I have done

$user = User::select('id',DB::raw("CONCAT(`first_name`, ' ' ,`last_name`) AS name"))
        ->where('name', 'like' ,$token)

which gives me an error Unknown column 'name' in 'where clause'. I printed out the raw sql which looks like this

select `id`, CONCAT(`first_name`, ' ' ,`last_name`) AS name from `user` where `name` like ? and `user`.`deleted_at` is null

How can I use concat and like with my eloquent builder properly?

answered question

3 Answers

2

try to use like that

$user = User::select('id',DB::raw("CONCAT(first_name, ' ' ,last_name) as name"))
        ->where('name', 'like' ,$token)

posted this
11

You should try this:

$user = User::select('Tenant_Id', DB::raw('CONCAT(first_name, " ", last_name) AS name'))
        ->where('name', 'like' ,$token)

posted this
12

Try This:

$user = User::select('id',DB::raw("CONCAT(`first_name`, ' ' ,`last_name`) AS name"))
        ->where(DB::raw("CONCAT(`first_name`, ' ', `last_name`)"), 'LIKE', "%".$token."%");

posted this

Have an answer?

JD

Please login first before posting an answer.