0

i want to search into users table by fullname which needs to concat two columns firstname and lastname and compare to the given value

 User::where(function($usersSearchQuery) use ($user,$fullName){
  $usersSearchQuery->whereRaw("CONCAT('first_name', 'last_name') = ?", [$firstName]); })->get();

i got this error

message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'full_name' in 'where clause' (SQL: select * from users where (1 = 1 and 1 = 1 and full_name LIKE sdfdf%))"

1
  • i replaced it with selectRaw() still the same error full_name is undefined Commented Sep 5, 2021 at 4:10

3 Answers 3

1

You can make a function in User model itself.

public $appends = ['full_name'];

public function getFullNameAttribute()
{
    return "{$this->first_name} {$this->last_name}";
}

// You can call it like this from controller or from view
$users = User::all();
foreach($users as $user) {
    $fullName[] = $user->full_name;
}

Sign up to request clarification or add additional context in comments.

2 Comments

Sandeep you are a little off. I have edited your answer to use the attributes as indicated in laravel documentation. Please wait for the edit to be approved.
the function already exists ! but how to do it in query because that's not optimised at all to add an additional code just for getting all the fullnames
0

I think you should change your select to ->select(DB::raw("CONCAT(first_name, ' ', last_name) as full_name")), after that, it should work. Or (if you are using Laravel 8), you can directly use selectRaw.

If it is still not working, change where with whereRaw like ->whereRaw('full_name LIKE "?%"', [$fullName]);. So have both selectRaw and whereRaw.

User::where(function ($usersSearchQuery) use ($user, $fullName) {
    return $usersSearchQuery->selectRaw("CONCAT(first_name, ' ', last_name) as full_name")
        ->whereRaw('full_name LIKE "?%"', [$fullName]);
})->get();

2 Comments

i update the code please check it , i got this error : message: "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Julianne% full_name)' at line 1 (SQL: select * from users where (1 = 1 and 1 = 1 Julianne% full_name))"
message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'full_name' in 'where clause' (SQL: select * from users where (1 = 1 and 1 = 1 and full_name LIKE "Julianne%"))"
0

The error exist in

->whereRaw('full_name','LIKE',$fullName.'%');

since the query output of the above eloquent query is

select CONCAT(first_name, ' ', last_name) as full_name from users
where full_name like 'SANDEEP%';

In this case the "full_name" attribute is not yet known to the query and it is not a column of users table, so the error says column not found, for this reason it is good to use sub query

Select * from (select CONCAT(first_name, ' ', last_name) as full_name from users) as u
where full_name like 'SANDEEP%';

whose equivalent query is

$sub = DB::table('users as u')->selectRaw("u.*, CONCAT(first_name,' ',last_name) as full_name");
$users = DB::table(DB::raw("({$sub->toSql()}) as sub"))
         ->whereRaw("full_name LIKE '{$fullName}%'")
         ->get();

4 Comments

thank you @Sandeep Dhakal, your code works but it's more optimised to put it all in one query
Then making a view in database with full_name column, and directly calling the respective modal would be a nice and clean way to do it in a single line.
i am using your solution actually it works fine i will accept this answer, there is only one thing it's returning only the fullname in the results how can i return the whole user ? not only fullname
I have edited the code to get all users data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.