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

Switch to MySQL/Postgres0:00

All right, next up in Laravel 9, we have support for a new database engine when working with Laravel Scout. Now, if you're unfamiliar, Laravel Scout has been around for a while, and it's basically a driver-based solution for performing full-text search on your Eloquent models. And up until this point, you also needed to use a service like Algolia or Melee Search to perform and execute the indexing and the search. But yeah, now, if you're working on an application that's not too big, you can probably get away with the database engine. So let's see how. Now, first up, in order for this to work, we need to be using either MySQL or Postgres. And up until this point, we've been using SQLite. So let's switch that back over, and then I will return the database name, and we'll just call it L9 for Laravel 9. All right, let me create that database. CREATE DATABASE L9; All right, we have it. And now I can run

Seed Posts for Search0:44

and then I will return the database name, and we'll just call it L9 for Laravel 9. All right, let me create that database. CREATE DATABASE L9; All right, we have it. And now I can run migrate. And also note while we're here, it is migrating that post table example from a couple episodes ago. So why don't we continue working with that to perform our search? So what I'll do here is you'll see I already have a PostFactory that consists of a userId, a title, and a body. So why don't we whip up a bunch of records? We'll say app/models/PostFactory.php, and I want a thousand posts in my database. Cool. We're all set to go. And real quick, if I open up TablePlus, here's all of our records. Okay, now I want to perform a search, and I'm going to grab some word that I can't pronounce. How about this one right here? And we will search for that. All right, so the first step is to install Laravel Scout. composer,

Install and Configure Scout1:38

and I'm going to grab some word that I can't pronounce. How about this one right here? And we will search for that. All right, so the first step is to install Laravel Scout. composer require laravel/scout. Next, I'm going to run vendor:publish, and we only want the one related to Scout. Where are you? Number eight here. Okay, so notice this copies a config/scout.php file. Let's have a look at that. So a few things to be aware of. First up, SCOUT_DRIVER. This is where you declare, are we using Algolia? Are we using an in-memory collection, which is useful for local development? Are we using MeiliSearch, or however you pronounce it? Or are we using the new database engine? That should probably be listed there. Now, if you want, we can either update it inline here, or I can declare my environment variable. Why don't we take that approach? We'll say here, SCOUT_DRIVER=database. Okay, but while we're here, also a couple little things.

or I can declare my environment variable. Why don't we take that approach? We'll say here, $scoutDriver equals database. Okay, but while we're here, also a couple little things. If we scroll down, you probably would want to perform any potential data synchronization. It's not quite relevant here for the database driver, but if you are using something like Algolia, you'd probably want to turn on Scout::queue. Okay, and then there's some other things, like if we want to require a committed transaction, you can have a look at all of this. Cool. So now, the only remaining step is for any model that you want to be indexed, you just need to add the Searchable trait, Laravel\Scout\Searchable. So this will do a bunch of things, but you'll see here, it adds some macros to your Collection class, and it also sets up a searchable scope. So you can call methods like this directly on the model, searchable,

Run First Search Query3:12

things, but you'll see here, it adds some macros to your collection class, and it also sets up a searchable scope. So you can call methods like this directly on the model, searchable, unsearchable, and the same for the various relationships. Finally, you'll have some additional methods on your model like search. And this is the one we want. So let's give it a shot. We'll go to my homepage here. And let's just return it as json, I'm going to say Post::search(), and I'll paste in that term. And then we'll get the results. And let's see how it works. So we give it a run. And here's what we get. And notice, well, let's see, if we try to track it down, oh, you know what, I'm getting everything here. There's one more step I forgot. At least at the time of this recording, if we're going to use the database engine, we have to explicitly set the two searchableArray method, or the two toArray method on your model. Let's stick with to

Define Searchable Attributes4:04

at the time of this recording, if we're going to use the database engine, we have to explicitly set the two searchable array method, or the two toArray method on your model. Let's stick with the searchable array. This is where we declare all the attributes that should be searched when you provide a query here. So yeah, if you want to search for the title, and then also the body, then we would do this here. And I think this should solve the problem. Come back, give it a refresh. And how many results do we have? Yeah, now we have 92 results. And I bet we'll see that term in every single record. It works. Okay, now a quick little note here. Often when you're using Laravel Scout, it will default to the models toArray method. But I think when we're working with the database engine, again, at the time of this recording, you need to be explicit about this. So basically, behind the scenes, it will use something like where like to perform the query,

Database Driver Behavior Notes4:50

the database engine, again, at the time of this recording, you need to be explicit about this. So basically, behind the scenes, it will use something like where like to perform the query, or it even has support for full text indexing if you have that turned on. But yeah, think how cool this is. It's been what, just a few minutes, and now we have a very clean way to perform a search across all of our posts. And actually, when we're using the database engine, there's no traditional indexing required like you might have with Algolia. And you can even paginate it if you want. Same thing, but now we have a paginated collection of posts that specifically contain that search term. Pretty cool.

Install Laravel ScoutDatabase EngineDeclare Full Text Attributes

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