Full Text Search Using Scout And Algolia in Laravel 8

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
Full Text Search Using Scout And Algolia

Now today in this example,I will show you how to add full text search by using scout in your laravel8 application. Here we will be use scout ans algolia for perform full text search.Here we know Full Text Search Using Scout And Algolia in Laravel 8.

Now we just need to create an new account in algolia.com and then need to create one index where our data will store.

Just need to follow below step to add full text search in your application:

Install Laravel 8

Now we need to create laravel 8 fresh application.

composer create-project --prefer-dist laravel/laravel ScoutFullTextSearch
Install Scout Package

So now here we will run below command to install scout package.

composer require laravel/scout

config/app.php

Now need to add ScoutServiceProvider to providers array in app.php file like as below :

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

So let’s go ahead and publish the assets provided by the Scout library by using the following command.

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

Install Algolia

Now we need to install algolia package by running below command.

composer require algolia/algoliasearch-client-php
Package Configuration

Now we need to create new account in algolia.com. So if you haven’t account on algolia.com site then click here and create a new account : algolia.com.

Full Text Search Using Scout And Algolia

From above screenshot we can get app id and secret from your dashboard.

config/scout.php

Now add id and secret in scout.php file look like as below .

'algolia' => [
    'id' => env('ALGOLIA_APP_ID', '*******'),
    'secret' => env('ALGOLIA_SECRET', '******************'),
],

.env

Also need put id and secret in .env file .

ALGOLIA_APP_ID=*********
ALGOLIA_SECRET=***********************

routes/web.php

Now need to add product route in web.php file.

Route::get('product', ['as'=>'product.index','uses'=>'ProductController@index']);
Route::post('product-add', ['as'=>'product.add','uses'=>'ProductController@create']);
Create Controller

By Run below command to make product controller

php artisan make:controller ProductController

app\Http\Controllers\ProductController.php

Now need to update productcontroller.php like as below :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Model\Product;

class ProductController extends Controller
{
    /**
     * Get the index name for the model.
     *
     * @return string
    */
    public function index(Request $request)
    {
        if($request->has('search')){
            $products = Product::search($request->search)->paginate(5);
        }else{
            $products = Product::paginate(5);
        }
        return view('product-index',compact('products'));
    }


    /**
     * Get the index name for the model.
     *
     * @return string
    */
    public function create(Request $request)
    {
        $this->validate($request,[
            'name'=>'required',
            'quantity'=>'required'
        ]);

        $products = Product::create($request->all());

        return back();
    }
}
Create Model

By Run below command to make product model

php artisan make:model Product

app\Model\Product.php

update product.php like as below :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Product extends Model
{
    use Searchable;

    public $fillable = [
        'name','quantity'
    ];

    /**
     * Get the index name for the model.
     *
     * @return string
    */
    public function searchableAs()
    {
        return 'product';
    }
}
Create Blade File

Now just Put below code in your product blade file .

product-index.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 8 Scout Search</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h2 class="mt-5">Laravel 6 Scout Search</h2><br/>
            <form method="POST" action="{{ route('product.add') }}" autocomplete="off">
                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                <div class="row">
                    <div class="col-md-4">
                        <div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
                            <input type="text" id="name" name="name" class="form-control" placeholder="Enter Name" value="{{ old('name') }}">
                            <span class="text-danger">{{ $errors->first('name') }}</span>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group {{ $errors->has('quantity') ? 'has-error' : '' }}">
                            <input type="text" id="quantity" name="quantity" class="form-control" placeholder="Enter Quantity" value="{{ old('quantity') }}">
                            <span class="text-danger">{{ $errors->first('quantity') }}</span>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <button class="btn btn-primary rounded-0">Create New Product</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="row mt-3">
        <div class="col-md-12">
            <h5><strong>Product</strong></h5><hr>
            <form method="GET" action="{{ route('product.index') }}">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <input type="text" name="search" class="form-control" placeholder="Search" value="{{ Request::get('search') }}">
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <button class="btn btn-primary rounded-0">Search</button>
                        </div>
                    </div>
                </div>
            </form>
            <table class="table table-bordered">
                <thead>
                    <th width="5%">Id</th>
                    <th>Name</th>
                    <th>Quantity</th>
                </thead>
                <tbody>
                    @if($products->count())
                        @foreach($products as $key => $value)
                            <tr>
                                <td>{{ ++$key }}</td>
                                <td>{{ $value->name }}</td>
                                <td>{{ $value->quantity }}</td>
                            </tr>
                        @endforeach
                    @else
                        <tr>
                            <td colspan="4">There are no data.</td>
                        </tr>
                    @endif
                </tbody>
            </table>
            {{ $products->links() }}
        </div>
    </div>
</div>
</body>
</html>

Read Also : Add Charts Using Highcharts In Laravel 8

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →