Full Text Search in Laravel 5 Example

Full Text Search in Laravel 5 Example

In this post we will give you information about Full Text Search in Laravel 5 Example. Hear we will give you detail about Full Text Search in Laravel 5 ExampleAnd how to use it also give you demo for it if it is necessary.

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.

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 IlluminateNotificationsNotifiable;

use IlluminateFoundationAuthUser as Authenticatable;

use NicolaslopezjSearchableSearchableTrait;


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

];

}

Also see:Dynamic Autocomplete search using Bootstrap Typeahead JS Example

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 AppHttpControllers;


use IlluminateHttpRequest;

use AppUser;


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

Also see:Laravel 5.5 CRUD Example from scratch

<!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 >

<h1>Laravel 5.5 Full Text Search Example</h1>


<form method="GET" action="{{ url('my-search') }}">

<div >

<div >

<input type="text" name="search" placeholder="Search" value="{{ old('search') }}">

</div>

<div >

<button >Search</button>

</div>

</div>

</form>


<table >

<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…

Hope this code and post will helped you for implement Full Text Search in Laravel 5 Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

  +  18  =  25

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US