![]() ![]() ![]() 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. In the next part of this series, you’ll learn how to limit the number of results in a query, and how to paginate results in Laravel Eloquent.Build better software and get user feedback directly in GitHub, GitLab, and more. Then, reload the main application page on your browser: You’ll obtain a page like the following, showing the total number of links included in each list, on the top menu: foreach ( $lists as $list ) slug) }}" title = " ) endforeach Locate the paragraph styled with the subtitle class, which contains the foreach loop that renders the application menu. Open the file resources/views/index.php in your code editor: resources/views/index.php In this part of the series, you’ll update the main application view to show the total number of links in each list. These are all made available through the inherent query builder that is built into every Eloquent model. Eloquent offers a few different aggregate methods that return a scalar number instead of an Eloquent object, including count(), max(), and sum(), among others. When working with database query results, it is often useful to obtain only the total number of rows in a result set, instead of pulling the full dataset content with a regular query. ![]()
1 Comment
2/1/2023 09:12:23 pm
Outstanding article... Thanks for sharing
Reply
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |