This episode is for members only

Sign up to access "Laravel Mentions" right now.

Get started
Already a member? Sign in to continue
Playing
14. Searching users with Laravel Scout

Transcript

00:00
Okay so let's improve on our username search or our user search using something like Darufel Scout. We don't really have an excuse not to use this because we have free solutions that we can actually use as our search service. For example MeliSearch. That's exactly what I'm going to be using in this episode. So the first thing to do is make sure you have MeliSearch installed. I'm
00:20
going to go ahead and start this instance inside of my terminal within this project. So all I'm going to do is just run MeliSearch. That's all I need to do and now what we want to do is go ahead and install Darufel Scout and if you've not worked with Darufel Scout before this will basically take all of the data from our users table or any other model that we want. It will
00:42
index it into MeliSearch and then we can go ahead and conveniently and more importantly quickly query on this data. So let's go ahead and get this installed and I'll guide you through it. So let's install Darufel Scout first of all and we'll go ahead and publish the config file just in case we want to change anything about that. So once this is done we'll go ahead and publish that
01:04
and next up we really just need to go ahead and add the searchable trait to any of the models that we want to go ahead and index. So let's close this off let's go over to our user and let's go ahead and index this. Now we need to be careful here and I'll show you why. So we need to be really careful with what we actually go ahead and index. Now that we've added searchable to this we
01:27
need to go ahead and set over in EMV the scout information. So if we come over to config and scout and we have a look at what we need here we've got a scout driver which by default is set to Algolia. I'm going to set that to MeliSearch and then we have inside of here if we just look for MeliSearch configuration we have a MeliSearch host and a MeliSearch key. So I'm going to set
01:51
this to the exact same thing here localhost 7700 and we can change that if we need to and we've got a MeliSearch key as well which by default when we're running this locally will be something like master key. So we'll see if this works and if it doesn't go ahead and change it. So if we open up localhost 7700 what you'll be presented with is a local version of your
02:13
MeliSearch index. So what we can do is we can choose an index which we don't have at the moment we can even start to search for stuff and we can basically see all the data that's indexed here. So let's go ahead and index this user data now that we've added that searchable trait. To do this this will happen automatically when users get created so anytime any user of your
02:35
users now register they will be inserted into this index which we will search on. So you don't need to worry about running this command all the time all of your data will stay in sync you'll just need to do an initial command like this. So we're going to say php artisan scout import and we're going to choose the model that has the searchable trait on with the data that we want to index which
02:57
is our user. Okay so this is errored let's just have a look at why that is and we just need to install the MeliSearch php library and we should be good to go. So let's do a composer require on that and we'll go ahead and rerun that artisan command to index our data and we are good. So that's all of that data now indexed into our MeliSearch search engine which is great so I can
03:22
now just start to search and it will give me back the results and of course you can see the time that is spent on this search is a lot quicker. Now can you see the problem here? Well the problem is that we are indexing probably data that we shouldn't. Now that's fine because we're going to control what gets returned from our controller as we do this but we probably don't want to index
03:45
all of this data. If you do that's fine but bear in mind that this data is public. So what we're going to do is we're going to customize within the user model the attributes that are actually inserted to our index. So where do we want to do this? Let's just do this before this relationship. So to do this we create our a two searchable array method which of course returns an array
04:11
and this is just the data that you want to index. So in our case we really just want to index the user's name and the user's username. That's really all the data that we need here to search on and also reveal publicly. So let's say username. Now to sort of refresh this if we go ahead and do a scout import again and give that a refresh you can see sure enough this is now updated. So we've got
04:38
the data inside of our search engine. How do we go ahead and search using our user search here? Well it's very simple. Laravel Scout is a first-party package it is incredibly easy to use. So I'm going to go ahead and just let's comment this out and let's go ahead and say instead of user query let's say user search which is a method that has now been added as part of this trait
05:07
this searchable trait that we've added on here. As you can see we just need to pass in a query. That query is of course request and get queue and by default we'll just use an empty string. That is all we need to do. Let's head over and see the difference and you can see that we get slightly different results because of course we are taking into account the name as well as the
05:28
username. That is because our index is now looking at both of these values to search which is a good thing and there we go. So now what we have is a much better we have an initial refined list which is fine but we have a much better and we have a faster search experience as well. So really simple change we're running this locally so you know we don't have to pay for any services for this to
05:53
work. We've imported this data this will now stay in sync so as users are created and deleted they will be pushed into our index and we now have a nice fast search experience. You can tweak this further do a little bit of research customize what you show but basically now we have the ideal solution to search for usernames as we start to mention people.
15 episodes1 hr 52 mins

Overview

Add mentionable functionality to your Laravel applications and mention users, projects, issues… literally anything.

We’ll start by setting up a simple comment system with Livewire, then detect, sync, notify and test mentions step-by-step. Not using Livewire? Don’t worry, the core functionality works with any stack.

If you are using Alpine/Livewire, we’ll add mention support to textareas to get a list of users we’re able to mention when we hit a trigger key.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!