In this episode, we're tackling how to efficiently index users for the mention ("@username") functionality in our app, without slowing everything down. We start off by discussing why it's a bad idea to load every user onto the frontend (spoiler: it's bad for performance!), and instead set up a dedicated user index in MeiliSearch. This special index includes only the bare essentials: a label and value for each user, making them easy and fast to search.
To keep things organized and secure, we create a new model called UserMention
that uses the existing users table, but is specifically for indexing only the info we want in Scout/MeiliSearch—rather than exposing all user info. This lets us customize exactly what gets indexed and keeps the data structure nice and clean.
We then walk through setting up Laravel Scout to create the index, add the right data, and ensure that any changes to user info (like a username change) are reflected in our search index. This is done by wiring up model observers—a super handy way in Laravel to react to events like a user being created or updated.
By the end of this episode, users are added to the mention index both when they register and when their info changes. This keeps your mentions feature lightning-fast, up-to-date, and scalable, so you can @ people to your heart's content!