In this episode, we tackle the problem of searching for users to mention (like using the @ symbol) in a scalable way. Originally, our app just loads and dumps every user in one giant list, which is just not practical or private as the userbase grows. We talk through the performance and privacy downsides of this approach and get ready to upgrade it.
We start by seeding the database with a ton of users—like 5,000—to simulate a real-world, busy app. Then, we look at how to refactor the frontend so that, instead of always having every user loaded, we make a search request as the user types. This involves tweaking our mentionables component to call a local userSearch
function on every keystroke, which will in turn query an API endpoint we set up.
On the backend, we build a really simple controller and endpoint (just using regular Laravel Eloquent queries) that takes in the query and responds with a filtered list of users. We make sure the data structure coming back works with our frontend dropdown.
Then, we connect the frontend search box to the backend endpoint using Axios, so that as you start typing (for example, @alex), the component reacts and updates the suggestions live—without ever exposing every username at once. We also tweak some attributes so the display and selection works smoothly.
This gets us a basic, much more scalable username search! Next up, we’ll look at making it even better with Laravel Scout for more advanced searching, and then wrapping this up in a reusable plugin.