Full Text Search in Laravel 5 Example

[et_pb_section bb_built=”1″ admin_label=”section”][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

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.

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.

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


[
'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


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




    Laravel 5.5 Full Text Search Example
    
    
    
    



	

Laravel 5.5 Full Text Search Example

@if($users->count()) @foreach($users as $user) @endforeach @else @endif
Id Name Email
{{ $user->id }} {{ $user->name }} {{ $user->email }}
Result not found.

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

Leave a Reply

Your email address will not be published. Required fields are marked *