Hello Artisan,
In this laravel scout example, I will show you how to search full-text data using scout in Laravel 9 example. In this Laravel 9 full-text search example, I will use the database driver as a scout driver. You know that Laravel 9 support database scout driver.
So in this example, I am going to use the database driver as a scout driver. We can use algolia
, tntsearch
etc as scout driver. But in this example, we are going to use the database
driver.
So from this tutorial, You will learn how full-text search work in Laravel and how we can implement it in Laravel 9 step by step. I will create a Post
model where I will use the title
field as a scout search key.
So if you don't know how to implement Laravel 9 full text search with scout, then this example is perfect for you. You have to just follow some few steps to complete this Larave; 9 full text search.
Step 1: download Laravel 9
This is the first step, and we need a fresh Laravel 9 app in this step to create a full-text search example in Laravel. So you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Create Model
In this step, we need a model to search our data from the database. So create a model like below:
php artisan make:model Post -m
And now update the migrations like:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
And update the model like:
app\Models\Post.php
namespace App\Models;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Post extends Model
{
use HasFactory, Searchable;
protected $fillable = [
'title'
];
public function toSearchableArray()
{
return [
'title' => $this->title
];
}
}
Now run php artisan migrate
command to create our posts
table.
Step 3: Install Scout
In this step, we have to install scout package and we will publish them:
composer require laravel/scout
Next, we have to publish our configuration file. so you need to run the below command:
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
Now we need to set the configuration database as a driver in your env file:
.env
SCOUT_DRIVER=database
Read also: Laravel 9 Queue Beginner to Advanced Example Tutorial
Step 4: Create Seeder
In this step, we will create some fake data in our posts table. So create seeder like:
php artisan make:seeder PostSeeder
And now update:
Database\Seeders\PostSeeder.php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Post;
use Faker\Factory as Faker;
class PostSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Post::truncate();
$faker = Faker::create();
foreach(range(1,100) as $index){
Post::create([
'title' => $faker->sentence(),
'created_at' => $faker->dateTime()
]);
}
}
}
And now run this command php artisan db:seed
to create our 100 fake posts. Now we have to import those records into scout. So run
php artisan scout:import "App\Models\Post"
Step 5: Create Controller
At this point, now we should create a new controller as PostController
. In this controller, we will add an index method, that will return users with a filter.
Let's update the following code to your controller file:
app/Http/Controllers/PostController.php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index(Request $request)
{
$posts = $request->filled('search') ?
Post::search($request->search)->get()
: Post::get();
return view('welcome', compact('posts'));
}
}
Step 6: Create Route
In this is a step we need to create a route for listing users. so open your "routes/web.php" file and add the following route.
routes/web.php
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
Route::controller(PostController::class)->group(function () {
Route::get('/posts', 'index');
});
Step 6: Create View
In the last step, let's create users.blade.php(resources/views/users.blade.php) for layout and we will write design code here and put the following code:
resources/views/welcome.blade.php
Now boom, everything is set to go. So now run this to command php artisan serve
and test it by visiting the below URL:
Read also: Laravel 9 Drag and Drop Image Upload Using Dropzone
Hope this Laravel 9 full text search example will help you to create search functionality in your application.
#laravel #laravel-9x #laravel-full-text-search #laravel-scout