Laravel 5.5 - Algolia Full Text Search Example With Scout

Today, we are share with you howt o built a full text search in laravel application using scout and algolia package. in many laravel application you need to built full text search. so, you can easyly done with laravel scout and algolia package.

algolia package provide a live search APIs funnction using this you can search full text search in model or quesry. in this tutorial we are show you one example how to built full tect search and how to integrate laravel scout and algolia.

Simple follow this step.

Step : 1 Install Require Package

Built full text search functionality we have required following two packages. so, install both of this by run following command.

We have install two following package.

1) laravel/scout

2) algolia/algoliasearch-client-php


composer require laravel/scout
composer require algolia/algoliasearch-client-php
	
Step : 2 Configure Package

After install successfully package we are should be configure this package service provider and alias in config/app.php file.


'providers' => [
	....
	Laravel\Scout\ScoutServiceProvider::class,
],

After set service provider then run following command after run tis command you can see one new file scout.php in config folder.


php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
	
Step : 3 Create Algolia Account

Next, we have required create one algolia account and get ALGOLIA_APP_ID and ALGOLIA_SECRET. you can create algolia account on click from here Algolia.com

Please, show following screenshot for how to get both of keys.

After get both of keys then opwn your .env file and set into it like that.


ALGOLIA_APP_ID=algolia_app_id
ALGOLIA_SECRET=algolia_app_secret	
	

Note :
We have already user table and User model so we are make search functionality on it.

After Done this tutorials youurr output look like this.

Scout and Algolia provide full text search. so, if you make scout searchable in any model then you can search all fields of this model from the one text box. how it work let's see.

Step : 4 Change In User Model

Next, make some following change in app/User.php file like that.


namespace App;

use Illuminate\Notifications\Notifiable;
use Laravel\Scout\Searchable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    use Searchable;

    /**
     * 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',
    ];

    public function searchableAs()
    {
        return 'users_index';
    }
}	
	

Add some dymmy records in users table run by following command.


php artisan tinker
>>> factory(App\User::class, 50)->create();
	

After add some records and changes in app/User.php file run following command in your terminal for index that records.


php artisan scout:import "App\User"
	

After run this command then open your algolia dashboard and show ther your all users table records indexed.

Step : 5 Create Route

Next, open your web.php file and add following one route in it.


Route::get('user-lists', 'SearchUserController@userList')->name('user-lists');	

Step : 6 Create Controller

Next, we have create SearchUserController.php controller in app/Http/Controllers folder.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Redirect;
use App\User;

class SearchUserController extends Controller
{    
    public function userList(Request $request)
    {
        if($request->has('search')){
            $users = User::search($request->search)
                ->paginate(6);
        }else{
            $users = User::paginate(6);
        }
        return view('user-search',compact('users'));
    }
}
	
Step : 7 Create Blade File

Next, we have create user-search.blade.php file in resources/views/ folder.


@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Data Table Demo</div>
                
                <div class="panel-body">
                    <form method="GET" action="{{ route('user-lists') }}">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <input type="text" name="search" class="form-control" placeholder="Enter User Name or Email For Search" value="{{ old('search') }}">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <button class="btn btn-success">Search</button>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table class="table table-hover table-bordered table-striped datatable" style="width:100%">
                        <thead>
                            <tr>
                                <th>Id</th>
                                <th>Name</th>
                                <th>Email</th>
                            </tr>
                        </thead>
                        <tbody>
                            @if($users->count())
                                @foreach($users as $key => $item)
                                    <tr>
                                        <td>{{ ++$key }}</td>
                                        <td>{{ $item->name }}</td>
                                        <td>{{ $item->email }}</td>
                                    </tr>
                                @endforeach
                            @else
                                <tr>
                                    <td colspan="4">There are no data.</td>
                                </tr>
                            @endif
                        </tbody>
                    </table>
                    {{ $users->links() }}
                </div>
            </div>
        </div>
    </div>
</div>
@endsection	
	

Now we are ready to run our example so run bellow command ro quick run:


php artisan serve

Now you can open bellow URL on your browser:

	
http://localhost:8000/user-lists

If you want to any problem then please write comment and also suggest for new topic for make tutorials in future. Thanks...

 Prev post

Next post