I have a user model with the following fields:
protected $fillable = [
'name',
'email',
'password',
'birthday',
'surname'
];
I created a search bar to search the users. I'm using Laravel scout to query the users like this:
$matchingUsers = User::search($request->search)->get();
If I search with just the name or just the surname, the result is fine. The problem is when introducing the name and the surname. In this case, I get no results.
Expected result
Given the following records in my database:
-[id=>1 , name => "Neil", surname => "Armstrong"] -[id=>2 , name => "Juan Manuel", surname = "Armstrong"]
If I introduce "Neil Armstrong" I expect to get the user with id = 1.
If I search for "Juan Armstrong", I expect to get user with id = 2.
If I search for "Manuel", I expect to get the user with id = 2.
What I have tried so far
Using raw SQL directly querying in the database manager I got the expected result for situation 1 using the following query:
SELECT id, name, surname, image
FROM users
WHERE CONCAT(name,' ', surname) = "Neil Armstrong";
For the other two cases, I've tried to use the orWhere() method. However, this doesn't seem to work together, and anyway mixing raw SQL with eloquent and Scout seems so dirty.
Is there any way to use Laravel Scout to do something like this? A solution using just Eloquent wold also be useful.