![]() ![]() ![]() This is what loads our related email addresses to be indexed. There are a few things to note.įirst, the secret to the method is the ->with('emailAddresses') relationship. What this method does is allow you to define what gets indexed and becomes searchable when your model is indexed. $customer = $this->with('emailAddresses') Step 2: Implement to Searchable Array or Make Searchable Usingįirst, let’s overwrite the toSearchableArray() on our model by adding the following: public function toSearchableArray() Whenever your model is updated, this method will be called and whatever is returned will be indexed by Meilisearch. When you use the Searchable trait you can overwrite the method toSearchableArray() on your model. When Meilisearch indexes this record, the email addresses will not be included. Say you have Meilisearch already configured on your Customer Model with a proper relationship to have many email addresses: hasMany('App\Models\CustomerEmailAddress', 'customer_id', 'id') The first step is to find the model you want to query a relationship with. Luckily, there’s a simple solution to include these relationships in your Meilisearch index. If the email addresses are in a different table, no results would be returned. When you search for customers with Laravel Scout in your app, allowing the user to search by an email address is crucial. The email addresses are located in a related table in your database, say customer_email_addresses. Each of those customers has multiple email addresses. The ability to search an index and get important related fields will make the UX of your app so much nicer! Why would you do this? This is a really quick article, but is something I implement on most of my Meilisearch instances. However, this will work with older versions of Laravel if needed. Along with that, I’m going to assume you are using Laravel Scout with Laravel 9. I’m going to assume you already have a properly functioning Meilisearch instance up and running. In this tutorial we will expand on the Laravel documentation and provide a few examples of how to include an Eloquent relationship’s model with a Meilsearch indexed record. Like everything with Laravel, there was already a solution available. When I first started to use Meilisearch with Laravel Scout, I wanted to query an Eloquent relationship along with my search. Build better software and get user feedback directly in GitHub, GitLab, and more. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |