Full Text Search in Laravel 5

By : PHPErrorCode October 8, 2017 1549 Views laravel

In this tutorial we will be learn create full text search in laravel 5.5, you like to learning? If Yes then you are a right place. In this tutorial i am going to share with you how to create full text search in your datbase table using "nicolaslopezj/searchable" composer package in laravel 5.5.

This is an full Search, Like you are search in "a" then if should filter with all the several columns like id, name, email, mobile, address, city etc. So don't worry about it here you will see full example of full text search from here.

After Done this tutorials youurr output look like this.

Full Text Search in Laravel 5.5

In this tutorial we will be learn to use "nicolaslopezj/searchable" composer laravel 5.5 package for full text search. now here we will make simple example below. Please take a look at the below step.

Step 1: Install composer Package

first Step is that we have to install "nicolaslopezj/searchable" composer package, so folow below command.

composer require nicolaslopezj/searchable

Step 2: Package Setup

Go to app/User.php this path and make some following change in User Model

<?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: create new Route for Search

Go to routes/web.php this path and add Route line below on your web.php file.

Route::get("search","HomeController@Search");

Step 4: Create Controller

Next, we have create HomeController.php controller in app/Http/Controllers folder and Now we will add Search() controller method in HomeController, So let's open HomeController and add following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home');
    }

    public function Search(Request $request)
    {
        if($request->has('search')){
            $users = User::search($request->get('search'))->paginate(4);
        }else{
            $users = User::paginate(4);
        }

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

Step 5: Create View File

Next, GO to resources/views folder and create a search.blade.php file then you have to add following code for search form and table data display.

<!DOCTYPE html>
<html>
<head>
    <title>Full Text Search in Laravel 5.5 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 5.5 Example</h1>

		<form method="GET" action="{{ url('search') }}">
			<div class="row">
				<div class="col-md-11">
					<input type="text" name="search" class="form-control" placeholder="Full Search" value="{{ old('search') }}">
				</div>
				<div class="col-md-1">
					<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
			<tr>
				<td colspan="3">{{ $users->links() }}</td>
			</tr>
		</table>

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

now we are ready to run this demo code, make sure you have some dummy data in users table and run bellow command in your commandpromt:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/search

I hope it can help you, Thanks...