


Here’s an example on how to do that with your customer model: public function toSearchableArray() With massive datasets, this is also preferred so you can scope down only what is important to search. Another solution is to only select the fields that you want searchable. You will have to remove these fields before your return statement at the end of your method. However, there could be other data that you don’t want index or returned with search results. First, a word to the wise, any sensitive data that requires extreme security should be encrypted in the database to begin with. This is REALLY easy to do if you don’t look out for it. Let’s talk a little bit about sensitive data. If you don’t have to worry about sensitive data on your models, then the makeAllSearchableUsing() method will work great! If you override the toSearchableArray() you can strip out any sensitive data you might not want. However, I wanted to show both for the reasons of sensitive data. I’d have to say this is the better option. In our example, this method would look like: protected function makeAllSearchableUsing($query) This will allow you to add relationships to your query before they are imported. The other option, you could also add the method makeAllSearchableUsing( $query ) to your model. That’s why we append the ->toArray() at the end of our query builder. Now whenever Laravel Scout sends the model to be indexed, the email addresses will get passed along and become searchable. 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.
ELOQUENT WORDS BEGINNING WITH E SOFTWARE
Build better software and get user feedback directly in GitHub, GitLab, and more.
