Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cclegend905's avatar

Advanced Search Query with Eloquent

I have a mobile phone specs website. I'm using the following query it returns results correctly:

$q = 'oppo reno';
Phone::where('name', 'like', sprintf('%%%s%%', $q))->get();

However, if we change the position of words of the search query to the following, it won't return any results.

$q = 'reno oppo';
Phone::where('name', 'like', sprintf('%%%s%%', $q))->get();

Any help is appreciated, thanks!

0 likes
8 replies
jlrdw's avatar

Do partial search, like %op%. Etc.

drehimself's avatar

Your current query is only searching for the text provided in the search, and any characters around it.

For example:

oppo reno or blah oppo reno blah

will match.

reno oppo will not match because oppo reno is not found within it.

What you need is a feature called "typo tolerance" and is one of the main reasons for using Laravel Scout: https://laravel.com/docs/8.x/scout.

Laracasts: https://laracasts.com/series/learn-laravel-scout

Algolia and MeiliSearch drivers are offered, the latter being free and open source.

Tray2's avatar

I take it you want to search for each of the words oppo and reno If so you can explode the search string and then loop over the array dynamically building the query and then execute it.

cclegend905's avatar

@Tray2 I already tried that. That would return unnecessary results. For example, if the query is "oppo reno" or "reno oppo" it would return all Oppo phones but the user only wanted to see reno series phones. Thanks for the suggestion.

Please or to participate in this conversation.