'Sort 'search results' based on the position of the 'search term' in them
I want to order the results showing those that start with the letters the user is typing first.
public $search = "";
public function render()
{
$business = Empresa::where("name", "like", "%" . $this->search . "%")
->orderBy("name")
->take(5)
->get();
return view('livewire.general.simpleSearch', compact("business"));
}
My website currently shows the results like this:
Instead, when the user types mer I want to display the results like this:
Solution 1:[1]
You try this
Empresa::where("name", "like", $this->search . "%")->take(5)->get();
Solution 2:[2]
You should not put % before your query
$business = Empresa::where("name", "LIKE", $this->search . "%")->orderBy("name")->take(5)->get();
In sql when using LIKE query % act as wildcard. If you put % before your search string it will search ending with your string, if you put % after your search string it will search starting with your string. Since you put % before and after your search string it will search every row contains your search query.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Swalih VM Bazar |
| Solution 2 | gguney |


