In this tutorial you will learn about the Laravel 7 Full Text Search Tutorial and its application with practical example.
In this Laravel 7 Full Text Search Tutorial I’ll show you how to implement full text search in laravel using full text search package. In this tutorial we will guide you step by step on how to implement full text search in laravel using full text search package. In this tutorial I’ll guide you with example of creating full text search with mysql DB in laravel app.
Laravel 7 Full Text Search Tutorial
- Step 1: Install Laravel New App
- Step 2: Configuration .evn file
- Step 3: Run Migration
- Step 4: Install Full Text Search Package
- Step 5: Add Fake Records in DB
- Step 6: Add Routes,
- Step 7: Create Controller
- Step 8: Create Blade View
- Step 9: Start Development Server
Step 1: Install Laravel Fresh Setup
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Configuration .env file
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Step 3: Run Migration
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Step 4: Install Full Text Package
In this step, we will install Full Text Package via the composer dependency manager. Use the following command to install Full Text Package.
1 |
composer require nicolaslopezj/searchable |
Step 5: Add Fake Records in DB
Now, we will generate some fake records using following command:
1 |
php artisan tinker |
After running the php artisan tinker. Run the following command. It will add 150 fake records in your database:
1 |
>>> factory(App\User::class, 150)->create(); |
Step 6: Create Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 |
Route::get('full-text-search', 'FullTextSearchController@index'); |
Step 7: Create Controller
Now, lets create a controller named FullTextSearchController using command given below –
1 |
php artisan make:controller FullTextSearchController |
Now go to app/Http/Controllers directory and open FullTextSearchController.php file. And add the following code into FullTextSearchController.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\User; class FullTextSearchController extends Controller { public function index(Request $request) { if($request->has('term')){ $data['lists'] = User::search($request->get('term'))->get(); }else{ $data['lists'] = User::get(); } return view('full-text-search', $data); } } |
Step 8: Create Blade View
In this step, we will go to resources/views/ directory and create one blade view file named full-text-search.blade.php file. Then add the following code into your full-text-search.blade.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Laravel 7, 6, 5 Full Text Search Tutorial</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"> </head> <body> <div class="container mt-5"> <div class="card"> <div class="card-header"> <h1>Laravel 7/6 Full Text Search Tutorial From Scratch</h1> </div> <div class="card-body"> <form method="GET" action="{{ url('full-text-search') }}"> <div class="row"> <div class="col-md-6"> <input type="text" name="term" class="form-control" placeholder="Search"> </div> <div class="col-md-6"> <button class="btn btn-primary">Search</button> </div> </div> </form> <br/> <table class="table table-bordered"> <tr> <th>Id</th> <th>Name</th> <th>Email</th> </tr> @if($lists) @foreach($lists as $list) <tr> <td>{{ $list->id }}</td> <td>{{ $list->name }}</td> <td>{{ $list->email }}</td> </tr> @endforeach @else <tr> <td colspan="3" class="text-danger">Result not found.</td> </tr> @endif </table> </div> </div> </div> </body> </html> |
Step 9: Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 2 3 4 5 |
php artisan serve If you want to run the project diffrent port so use this below command php artisan serve --port=8080 |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/full-text-search |
or
1 |
http://localhost/blog/public/full-text-search |