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

Adding Full-Text Index0:00

All right, next up, while we're on the subject of search, Laravel 9 also adds support for full-text indexing and WHERE clauses. So let's see what that means. Let's go back to my host table migration, and why don't we add a full-text index to the body column? I can do that in Laravel 9 by calling the fullText method. All right, so let's refresh my migrations. And actually, while we're here, real quick, let me go to my PostSeeder and quickly set this up so I don't have to manually recreate or repopulate my post models.

Reseeding Post Data0:25

let me go to my PostSeeder and quickly set this up so I don't have to manually recreate or repopulate my Post models. We'll say PostFactory. One more time, I want 1,000 Posts. Okay, so php artisan migrate --fresh. I want to seed as part of that. But in this case, my main seeder will be, what is it? database/seeders/PostSeeder.php. Like that. All right, so we should be up in business.

Testing whereFullText Query0:50

Like that. All right, so we should be up in business. But now, if I have a look at TablePlus, in my post table, if we look at the structure, notice we do have a full-text index. Okay, so let's play around with this. If I were to run or boot up php artisan tinker, I can now do things like Post::whereFullText('body', 'includes', $searchTerm)->get(); Let's see how many records match that.

How Full-Text Works1:13

includes that search term we were looking for earlier. Let's see how many records match that. Ninety-four. It's working. So yeah, behind the scenes, when you call WHERE full-text, it's not going to use WHERE like. That does have some performance limitations. It's going to use MySQL's MATCH AGAINST functionality. And that's mostly it. But yeah, to finish up, if we make a callback to the last episode where you learned about Laravel Scout,

Configuring Scout Full-Text1:34

But yeah, to finish up, if we make a callback to the last episode where you learned about Laravel Scout, by default, it's always going to use a WHERE like check. But if you have certain attributes that would benefit from a full-text check, you can use a php attribute like this. Search using full-text, and then you provide the name. And that's all there is to it. Pretty easy.

Blueprint fulltext() MethodBuilder whereFulltext()

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