In this episode, we're getting all of our current application data from the database indexed into a search engine, so it's ready to be searched lightning-fast! We're using MeiliSearch as our search engine because it's local, open-source, and super easy to set up—plus, later, you can swap it out for something like Algolia with basically no code changes thanks to Laravel Scout.
We start by installing MeiliSearch on our machines, then fire it up so it's running in the background and ready to accept data. After that, we jump into our Laravel project to install Laravel Scout, configure it for MeiliSearch, and add the necessary "Searchable" trait to our models (Users and Courses in this case). Once that's set, we use the Scout console command to batch import all existing data into the search index.
Next, we check out our MeiliSearch dashboard to see all the imported data. By default, all model fields get indexed, but that's usually way more than we need, so we show how to customize exactly what data gets sent using the toSearchableArray
method on the model. As an example, we add an avatar URL to our User model so that user avatars can show up directly in search results.
The best part? Once set up, any newly created or deleted models are instantly kept up to date in the search index—no manual commands required. So, after this session, your app's data is search-ready, and we're all set to make a snazzy front end with Vue that hooks right into MeiliSearch (that's coming up next!).