In this episode, we tackle improving our user search feature by integrating Laravel Scout with Meilisearch as our search backend. It's a solid upgrade that gives us blazing fast and relevant search results, and Meilisearch is free and really easy to run locally—no excuses not to try it out!
We start by spinning up Meilisearch right in our project directory, then install Laravel Scout and its Meilisearch driver. If you've never used Scout before, it indexes data from your models (like users) so searching becomes super efficient. After handling setup, we explore how to make specific models (like User
) searchable by adding the Searchable
trait. We go over why it's important to control what fields get indexed, since by default Scout might include everything (which could expose sensitive data). We fix this by customizing the toSearchableArray()
method to only include the fields we care about (like name
and username
).
With our user data indexed, we import the data, solve a minor dependency hiccup (installing the Meilisearch PHP package), and see how searches are instantly way faster. We then update our search logic in the controller to use Scout's search method, which is as easy as calling User::search($query)
. Boom—now our search covers both names and usernames lightning-fast!
To finish up, we look at the Meilisearch dashboard to see what our index looks like and test out the improved search in the app. As users register or are deleted, the index automatically updates, so you don’t need to manually keep things in sync. This setup gives us an ideal, robust, and efficient backbone for searching users (for mentions or whatever else) without needing any paid services or complicated infrastructure.