Full Text Search in Laravel 5 Example

By Hardik Savani April 16, 2024 Category : PHP Laravel Bootstrap

Are you want to make full text search in your laravel 5.5 application ?, If Yes then you are a right place. In this post i am going to share with you how to create full text search using "nicolaslopezj/searchable" composer package in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

In real field we almost require to create full text search like if you have users table and there are several columns like id, first name, last name, email, address, city etc. So if you search just "a" then if should filter with all the columns. So it's like just run simple query with search, it should work like datatables search. So don't worry about it here you will see full example of full text search from here.

In this example i am going to use "nicolaslopezj/searchable" composer package for full text search. they provide several other feature you can use on guide line, now here we will make simple example. So let's just follow bellow step.

Preview:

Step 1: Install Package

first thing is that we have to install "nicolaslopezj/searchable" composer package, so let's run bellow command for it.

composer require nicolaslopezj/searchable

Step 2: Package Setup

In this step, if i am not wrong we have default table "users" created with it's model. So, we have to simple add SearchableTrait on user model. So let's add SearchableTrait as like following:

app/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

Here, we will add new "my-search" route, so open web.php file and add one following route.

routes/web.php

Route::get("my-search","HomeController@mySearch");

Step 4: Add Controller Method

Now we will add mySearch() controller method in HomeController, So let's open HomeController and add following code:

app/Http/Controllers/HomeController.php

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use App\User;


class HomeController extends Controller

{


public function mySearch(Request $request)

{

if($request->has('search')){

$users = User::search($request->get('search'))->get();

}else{

$users = User::get();

}


return view('my-search', compact('users'));

}


}

Step 5: Create Blade File

At last we will create my-search.blade.php file and you have to add following code for search form and table data display:

resources/views/my-search.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 5.5 Full Text Search 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>Laravel 5.5 Full Text Search 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>

now we are ready to run our full example, make sure you have some dummy data in users table.

You can also get more information about "nicolaslopezj/searchable" package from here : Click Here

I hope it can help you...

Shares