0

I am trying concat first name and last name in Laravel but it's not working. i am using mongo db

i was tried this sql query but its not working

->orWhere(\DB::raw("CONCAT(`last_name`, ' ', `first_name`)"), 'LIKE', "%".$search."%"); 

can anyone find the alternate solution of concat in mongodb

Thank you in advance

madhav
  • 3
  • 3
  • what package are you using ? – Moussab Kbeisy Dec 23 '21 at 12:09
  • https://stackoverflow.com/questions/24662413/select-records-matching-concat-value-of-two-fields-in-mongodb says it can't be done with a regular query. I don't know how to use that with Laravel's query builder, though – aynber Dec 23 '21 at 12:37
  • Hello @MoussabKbeisy We are using a mongo package for laravel jenssegers/mongodb – madhav Dec 24 '21 at 05:38

1 Answers1

0

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);
});
kapitan
  • 2,008
  • 3
  • 20
  • 26
  • hi @kapitan thank you it's working – madhav Dec 24 '21 at 10:36
  • glad to know its working, though you might want to full proof this further by adding the `trim()` function. `trim(explode(" ", $search))`, `trim($full_name[0])` and `trim($full_name[1])`. – kapitan Dec 27 '21 at 00:12