Full Text Search in Laravel Example

Full Text Search in Laravel Example

In this example we will show full text search in laravel example. I will explain step by step full text search in laravel using nicolaslopezj/searchable package.

We will show laravel 8 full text search. In this post you can learn how to implement full text search in Laravel framework by using Searchable Laravel package. we will discuss on full text search laravel eloquent. how to make full text search in laravel application.

In this example i am going to use nicolaslopezj/searchable package for full text search. Laravel full text search is a one type of search method, which search data on every word of request in your database.

In real field we almost need to create full text search such as if you have users table and there are many columns like id, first name, last name, username, email, address, city etc. So if you search just “a” then if should filter with all the columns.

Read Also :  Laravel Eloquent whereBetween Example

Example : Laravel full text search

Step 1: Install Package

In this step we will install “nicolaslopezj/searchable” package in laravel using composer. So let’s run bellow command :

composer require nicolaslopezj/searchable

Step 2: Package Setup

In this step we have to simple add SearchableTrait on user model. So let’s add SearchableTrait as like following:

app/models/User.php

<?php


namespace App;


use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Nicolaslopezj\Searchable\SearchableTrait;


class User extends Authenticatable
{
    use Notifiable;
    use SearchableTrait;


     /**
     * Searchable rules.
     *
     * @var array
     */
    protected $searchable = [
        'columns' => [
            'users.name' => 10,
            'users.email' => 5,
            'users.id' => 3,
        ]
    ];


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];


    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Step 3: Add New Route for Search

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;


Route::get('my-search', [UserController::class,'mySearch']);

Step 4: Add Controller Method

In this step we will add mySearch() controller method in UserController, So let’s open UserController and add following code:

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function mySearch(Request $request)
    {
    	if($request->has('search')){
    		$users = User::search($request->get('search'))->get();	
    	}else{
    		$users = User::get();
    	}


    	return view('users', compact('users'));
    }

}

Step 5: Create Blade File

In this step we will create users.blade.php file and you have to add bellow code for search form and table data display:

<!DOCTYPE html>
<html>
<head>
    <title>Full text search in laravel example</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
</head>


<body>
    <div class="container">
        <h1>Full text search in laravel example</h1>


        <form method="GET" action="{{ url('my-search') }}">
            <div class="row">
                <div class="col-md-6">
                    <input type="text" name="search" class="form-control" placeholder="Search" value="{{ old('search') }}">
                </div>
                <div class="col-md-6">
                    <button class="btn btn-success">Search</button>
                </div>
            </div>
        </form>


        <table class="table table-bordered">
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Email</th>
            </tr>
            @if($users->count())
                @foreach($users as $user)
                <tr>
                    <td>{{ $user->id }}</td>
                    <td>{{ $user->name }}</td>
                    <td>{{ $user->email }}</td>
                </tr>
                @endforeach
            @else
            <tr>
                <td colspan="3">Result not found.</td>
            </tr>
            @endif
        </table>


    </div>
</body>
</html>

Step 6: config\database.php

In this step you can set 'strict' => false and add modes to "mysql" option.

Read Also :  Laravel 8 Yajra Datatable Example Tutorial

I hope you understand of full text search example and it can help you…