You need an 'and' since you only want matches for the name AND the surname.
I think a straight Where is applied as an And. Try changing orWhere to just Where.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a question, how do you go about using the where clause and performing concatenation. MySQL query: select * from users where concat(name, ' ', surname) LIKE '%John%' (using this as an example because if someone puts in "John Robinson" in the search box, we want them to see just that result. The query works fine in MySQL but I am having a hard time doing this in Laravel in the where statement. Part of my original statement that I am modifying received from different devs:
])->where ('name', 'LIKE', '%' . $keyword . '%') ->orWhere('surname', 'LIKE', '%' . $keyword . '%') ->get();
This gives us all Johns but if someone puts in John Robinson, it gives us only people with the last name Robinson. I am a noob and sorry if this is a stupid question.
Sorry, I was thinking your search terms were separate fields. Time to resort to a little DB::raw
$result = User::where(DB::raw('concat(name," ",surname)') , 'LIKE' , '%keyword%')->get();
It's unusual but you will get no search results if the user enters extra spaces anywhere, before, after or in the middle. Its an idea to replace all spaces with % before running the query.
Please or to participate in this conversation.