در حال بارگذاری ...

Introducing Scout and Algolia0:00

Okay, let's move on to Laravel Scout. So this is a driver-based tool for performing complex search queries on your Eloquent models. Now behind the scenes, out of the box, you have a null driver and an Algolia driver. But you could even create your own extension if you know how to do that. But Algolia will be fine. So you can go to algolia.com, sign up, you'll get a free trial. This is not a free tool, but you know what, I pay it without thinking. So at the very least, give it a shot. Okay, so we're going to start from scratch and cover this in two parts. In the first part, we're going to set everything up and simply create our first index and learn.

Loading Sample Database0:28

Okay, so we're going to start from scratch and cover this in two parts. In the first part, we're going to set everything up and simply create our first index and learn how we can save files and automatically update our Algolia index behind the scenes. So we're going to start with a sample database. We'll grab the popular Sequila database. And that's like a big movie film database. And it'll just give us something to work with. So if you're working along, you can go to this URL and download it. Next, if you scroll down, here's how you can install it. So let's do this.

Next, if you scroll down, here's how you can install it. So let's do this. I'm going to go to my code directory. And I've already downloaded that file behind the scenes. So I can say MySQL, the username is root, there is no password. And now we're going to import the Sequila database. And you'll see that there's two different files here. We'll start with the schema and that will create the tables. Okay, and then we're going to do another one for the data itself. Excellent.

Okay, and then we're going to do another one for the data itself. Excellent. So now if I switch over to SQL Pro, you'll see that we now have our Sequila database with tons of stuff here. Okay, let's do some setup. cd scout. This is literally a fresh install of Laravel 5.3. So I'm going to go to my .env file. And the database name is called Sequila. The username is root and there is no password.

Creating Models and Conventions1:43

And the database name is called Sequila. The username is root and there is no password. Okay, but next, I want to create a couple models to go along with those tables. So we have Film and Actor. Art, or I'm sorry, php artisan, make a model called Film. And then another one for the Actors in the Film. Okay, but real quick, this is actually maybe a good thing to know. When you're starting from scratch in Laravel, you can stick with common conventions. And one of those conventions is, well, the ID of any table will be named ID.

When you're starting from scratch in Laravel, you can stick with common conventions. And one of those conventions is, well, the ID of any table will be named ID. But in this case, the primary key is actually film_id. That's the convention they use. And in fact, for pivot tables, it's not each table in alphabetical order, like actor_film. It's actually the reverse. So with Eloquent, whenever you need to override those things, you can just set it like this. In our case, the table name is not films. It is film. So we need to be explicit about that.

It is film. So we need to be explicit about that. Next, like I said, the primary key, it's not going to be the default of id. It's going to be film_id. So we're going to override that. And then finally, if I switch back, it doesn't look like there's any timestamps here that we can use. So I will set public timestamps to false so that Eloquent doesn't try to set those. And again, this has nothing to do with Scout whatsoever. Okay, one more bit of setup. We have a relationship between film and actors.

Defining Model Relationships3:06

Okay, one more bit of setup. We have a relationship between Film and Actor. So we could say return belongsToMany. So basically, we have a Film and we have Actors, right? And Actors exist in Films through this pivot table. So the Actor with an ID of 1 starred in the Film with an ID of 1. Okay, so we can say that's a belongsToMany relationship. But once again, the table name is film_actor. So I need to be explicit about that. So that looks good.

So I need to be explicit about that. So that looks good. And then we'll just have to do basically the same thing with actor. And I'll paste this in to save us some time. The table name is actor and the primary key is actor_id. So now if I do php artisan tinker, I should be able to say, give me the first film. And we have it. We'll save that to F. And if I now say, give me the actors in that film, we get a collection of all the actors. Okay, so good job on the setup.

Installing and Configuring Scout3:54

And if I now say, give me the actors in that film, we get a collection of all the actors. Okay, so good job on the setup. Now we can get Scout installed. And we can push this table and all the records up to Algolia so that we can use Algolia to perform advanced full-text searching on it. Way more advanced than something you could write on your own. So here's the steps. We're going to require Scout. Now, Laravel Scout is not built in, just like Passport. It's not built in.

Now, Laravel Scout is not built in, just like Passport. It's not built in. We need to grab it ourselves. Next, while it's doing its thing, like most packages, we have to go to config/app.php and add the service provider here. And remember, that's sort of like a bootstrap. This is what bootstraps Scout with the Laravel framework. But real quick, always wait until your package is installed before you actually add it here. Otherwise, you might get an error.

before you actually add it here. Otherwise, you might get an error. Okay, so that looks good. ScoutServiceProvider, if you're curious, it adds a singleton for the engine manager. And that figures out what driver you're using. And then it registers an artisan command. And it also can publish a Scout configuration file. So let's do both of those now. php artisan.

So let's do both of those now. php artisan. And you'll see that, yes, we have one new command that we can use. And we'll do that at the end of the video. But next, if I publish our vendor files, it'll publish your notification email template, your pagination views, but also the config/scout.php file. So if we take a look at that, yeah, notice we're setting our default driver. So this is just like anything else you might use,

yeah, notice we're setting our default driver. So this is just like anything else you might use, like session or caching. It's a driver-based approach to full-text search. So out of the box, we're going to assume you want Scout. We can specify any Scout prefix that we want to use. We can determine whether it should be queued or not. And for production, very, very likely you want to set this to true. So even if you use the database queue driver to make it very simple, you want to have this set to true in real life.

So even if you use the database queue driver to make it very simple, you want to have this set to true in real life. And that way, you don't have to wait for these API calls to be made. It'll be done behind the scenes. And you can get back to the user as quickly as possible. And then finally, we have a section for the Algolia app ID and secret. So let's set that up. Now, I've signed up for a free account. Actually, in fact, ClaraCast uses this behind the scenes. And if we go to API keys, here's what we want.

Actually, in fact, ClaraCast uses this behind the scenes. And if we go to API keys, here's what we want. So this is the application ID. And we can store this within our .env file right here, Algolia app ID. Next, if we come back, it also wants the Algolia secret. So we'll grab that one. And then once again, store it here, Algolia secret. OK, we're just about ready to go. We only have to pull in one more package. Because we are using Algolia, Laravel and Laravel Scout will depend upon Algolia search client.

We only have to pull in one more package. Because we are using Algolia, Laravel and Laravel Scout will depend upon Algolia search client. So let's composer require algolia/algoliasearch-client-php. And specifically, their PHP client. OK, give that a minute to install. And then we're all set to go here. All of the setup is out of the way. So here's what we can do. I can now go to any model. For example, let's start with Film.

Indexing and Syncing Records7:13

I can now go to any model. For example, let's start with Film. And I'm just going to add the searchable trait to it. And this is something that, of course, Laravel Scout provides. So let's import that. Use Laravel Scout searchable. And now I'll show you what that's going to do. But real quick, let's just see it in action. We've declared that this Film is searchable. It adds a bunch of methods in the trait.

We've declared that this film is searchable. It adds a bunch of methods in the trait. So now I can say php artisan scout:import. And then I provide the name of the model. OK, so take a look what's going on here. It takes all of the records in that table. And then it chunks them into sets so that you don't explode with too much memory. So it chunks them into sets of 100. And each one of those gets pushed up to your Algolia index. And for now, just think of an index with Algolia or even Elasticsearch as a table.

And each one of those gets pushed up to your Algolia index. And for now, just think of an index with Algolia or even Elasticsearch as a table. It doesn't have to be that way. But for now, I think it's OK to think of them as somewhat synonymous. OK, so we go back to our indices. And you'll now see that that command gave us a new film index that has 1,000 records. Now, here's the cool thing. Behind the scenes, Scout uses a model observer to detect when you delete a record or when you add one. So for example, the objectId, that will be our primary key.

to detect when you delete a record or when you add one. So for example, the object ID, that will be our primary key. We could say php artisan tinker. We're going to find the Film with an ID of 999. OK, so that is Zoolander Fiction. That's this record. Let's just change the title. We're going to change it to Zoolander 2. Now, when I save it, you'll see that because we're not queuing things up, it'll take slightly longer, just a fraction longer, like a half second.

Now, when I save it, you'll see that because we're not queuing things up, it'll take slightly longer, just a fraction longer, like a half second. But now here's what's going to happen. Yes, it updated it in our database like usual. But it also fires off an API call to update this record with Algolia. So now if I give it a refresh, you'll see that, yes, it has been changed to Zoolander 2. And also, the same thing will be true if we were to delete this record. LayerFL will remove this item from the index so that it does not get returned in your search results. Now, let's take a look at what's really going on here, just in case you are curious.

so that it does not get returned in your search results. Now, let's take a look at what's really going on here, just in case you are curious. So we'll take a look at this searchable trait. What got added here? OK, well, first, bootSearchable. bootSearchable, this is a special method that Laravel will call just in case you need to do any instantiation-type work for your trait. Scout does three things in this case. It adds a global scope. So if we take a look at that and we scroll down, you can see that builder macro.

It adds a global scope. So if we take a look at that and we scroll down, you can see that builder macro. So you may not know this. And in fact, I don't think I've covered it yet at Laracasts. But for a number of things with the framework, they are made macroable. And basically, what that means is you can hook into them and extend their functionality. So for example, if I wanted to add a macro to Laravel's collection class, I could do that. And in this case, we're doing it to Laravel's builder class. And by the way, you don't need to know any of this if you don't want to. It's just if you're curious.

And by the way, you don't need to know any of this if you don't want to. It's just if you're curious. So we're adding a new searchable method to the builder, and then also an unsearchable method. And again, that would be like if you said, at film where some condition that would return a bunch of records delete. Yeah, in that case, it's going to remove all of the records from your index. OK. Next, it adds a new model observer. And this is the class that's actually responsible for listening for, if we scroll down,

Next, it adds a new model observer. And this is the class that's actually responsible for listening for, if we scroll down, when a new record is created, or when a new record is updated, or when a record is deleted. And notice it knows to use unsearchable in that case. But now notice that we're calling unsearchable and searchable. So if we take a look at that, here's the method. So let's see what happens. Let's go to create it. You say app\Film::create, and you pass some records. It saves it to the database.

You say app\Film::create, and you pass some records. It saves it to the database. But then this method gets fired because we're listening for the created event. So we're going to say model->searchable. If we come back, searchable just turns that into a collection. And then it calls a searchable method on the collection. So this is like Illuminate\Support\Collection searchable. Well, we know that doesn't exist by default. And the answer is, once again, that's added as a macro. Now, here we go.

And the answer is, once again, that's added as a macro. Now, here we go. So Collection, we're adding a new macro. Oh, real quick. There you go. So this trait, in fact, you can add it to your own classes if you want to. So if you ever want to allow for really dynamic extension of a class that already exists, yeah, you can pull in Laravel's Macroable trait. Anyways, though, so we're adding a new method called searchable. And when that's triggered, we're going to call queue make searchable.

Anyways, though, so we're adding a new method called searchable. And when that's triggered, we're going to call queue make searchable. And now in this case, config scout queue. So what it's doing here is it's looking into your scout file. And it's saying, are you using queues? So if you're not, then we're just going to do it in real time. Otherwise, we're going to dispatch a job. And that job, if we take a look at it, it's just going to do the exact same thing. So if we take that method out, and we take a look at this, it's basically the same thing. Grab the first model, searchable using Algolia in our case.

So if we take that method out, and we take a look at this, it's basically the same thing. Grab the first model, searchable using Algolia in our case. And then we call an update method. So if I take a look at the Algolia engine and update, here's the basic code. If you've ever used Algolia directly, yeah, this is the type of code you would write. So initialize a new index. Now right here, this is how Laravel is determining what the name of the index should be. So imagine it's basically this. For any model, call a searchableAs method. And you can override this, by the way, if you need to.

For any model, call a searchable as method. And you can override this, by the way, if you need to. But by default, it's going to grab a prefix. We don't have a prefix. So we're just going to return the name of the table. And in our case, the name of the table is film. So that is what gets saved if we scroll up right here. OK. Anyways, if we come back, we initialize an index. And then with Algolia, you can call an addObjects method, where we map over the models.

Anyways, if we come back, we initialize an index. And then with Algolia, you can call an addObjects method, where we map over the models. So in this particular case, it's just a single model. It's turned into a collection just to make the API consistent to work with, whether you have a bunch of models or just a single. Anyways, and then it just returns an array. So add these items to the index. And specifically, this is what's going to be added. So notice we merge the objectID. This is something Algolia often wants.

So notice we merge the object id. This is something Algolia often wants. And it's just usually the primary key. It's an identifier. And then it calls toSearchableAsArray. That is just going to determine what gets returned and what gets saved to the index. So by default, yeah, it's just going to cast the model to an array. But if you want to override that, then once again, you can create this method on your model. Like so. And then you can return specifically and exclusively what you want.

Like so. And then you can return specifically and exclusively what you want. So for example, if you only want to save the title, then you could save this title. And that's all that's going to be submitted. Let's try it again. php artisan scout:import. And we're failing probably just because I changed this. OK. One more time.

More often than not, yeah, you can just stick with the default implementation. So now that we have Film, we can do the same thing for the Actors. Ah, and now in this case, it's failing because it's trying to call a method. Now, what's going on here is we have not designated that this model is searchable. So we're going to do that now and then import this at the top. That will register all the macros. That will handle the model observing. And it'll also give us methods like this, makeAllSearchable. If we run it again, we only had 200. So if I come back to Chrome and give this a refresh,

So now in part two, well, how exactly do we query this API? Well, we can tackle all of that in the next episode. I'll see you then.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟