Laravel 5.5 – Algolia Full Text Search Example With Scout

Laravel 5.5 – Algolia Full Text Search Example With Scout

In this post we will give you information about Laravel 5.5 – Algolia Full Text Search Example With Scout. Hear we will give you detail about Laravel 5.5 – Algolia Full Text Search Example With ScoutAnd how to use it also give you demo for it if it is necessary.

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' => [
	....
	LaravelScoutScoutServiceProvider::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="LaravelScoutScoutServiceProvider"
	


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 IlluminateNotificationsNotifiable;
use LaravelScoutSearchable;
use IlluminateFoundationAuthUser 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(AppUser::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 "AppUser"
	


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

use IlluminateHttpRequest;
use IlluminateSupportFacadesInput;
use Redirect;
use AppUser;

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 >
    <div >
        <div >
            <div >
                <div >Data Table Demo</div>
                
                <div >
                    <form method="GET" action="{{ route('user-lists') }}">
                        <div >
                            <div >
                                <div >
                                    <input type="text" name="search"  placeholder="Enter User Name or Email For Search" value="{{ old('search') }}">
                                </div>
                            </div>
                            <div >
                                <div >
                                    <button >Search</button>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table  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…

Hope this and post will helped you for implement Laravel 5.5 – Algolia Full Text Search Example With Scout. 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 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 *

2  +  6  =  

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