Laravel Scout Algolia Full Text Search Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Today now in this tutorial,i will tell you how can full text search by using scout algolia. So here i will show the example of laravel scout algolia full text search. Here you can full text search by using scout algolia api. Now it can be this example of full explain scout algolia full text search.

From here I will show you the basic step of scout algolia api full text search. So if you can use full text search for install scout and Algolia api package. Now we are by using algolia

So api by using full text search example in laravel.

Now here the following steps example laravel full text search by Using scout algolia

Step 1: Create Laravel Project

Now In this step we will create laravel project following command.

composer create-project --prefer-dist laravel/blog
Step 2: Database Configuration

Then after the create laravel project , we need to make database configuration, So you have to add following details on your .env file.

1.Database Username

2.Database Password

3.Database Name

Now in .env file also available host and port details, we can configure all details as in our system, So we can put like as bellow:

following path: .env

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Step 3: Install Scout Package

Now in this step we will install scout package by using blow the command.

composer require laravel/scout

So after installing Scout, we should publish the Scout configuration by using the vendor: publish Artisan command.

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

By Running a queue worker will allow Scout to queue all operations that sync your model information to your search indexes, by providing much better response times for your application’s web interface.

So need to Set the value of queue in the .env file.

following path: .env

SCOUT_QUEUE = true
Step 4: Install Algolia Driver.

Now we will also want to install the algolia php sdk via the composer package manager,

composer require algolia/algoliasearch-client-php

So now next, we need to set id and secret of Algolia. So move to this website Algolia and then create your account.

And after the login, we can get id and secret from this link: https://www.algolia.com/api-keys

Laravel Scout Algolia Full Text Search Example

Now we can set id and secret in our .env file.

Set the value of queue in the .env file.

following path: .env

ALGOLIA_APP_ID = Enter your Application ID
ALGOLIA_SECRET = Enter your Admin API Key
Step 5: Create Posts Table and Model

Now in this step we have to create a new migration for posts table by using Laravel project php artisan command, so first need to fire bellow command:

php artisan make:model Post -m

So after this command we have to put bellow code in our migration file for create posts table.

following path:/database/migrations/2020_01_10_102325_create_posts_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->text('body');
            $table->string('category');
            $table->integer('view');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Now we need to run migration be bellow command:

php artisan migrate

So after that we have to put bellow code in your model file for create Post table.

following path:/app/Model/Post.php

<?php

namespace App;

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

class Post extends Model
{
	use Searchable;

    protected $fillable = ['title','body','category','view'];
     /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function searchableAs()
    {
        return 'post_index';
    }
}
Step:6 Batch Import

Now in this step need to insert some records to database records need to import into your search driver. So scout provides an artisan command that we may use to import all of our existing records into our search indexes.

php artisan scout:import "App\Post"
Step 7: Create Route

Now in this is step we need to create route for post search layout file

following path:/routes/web.php

Route::get('index','SearchController@search');

Step 8: Create Controller

So here now in this step we should create a new controller as SearchController,So just need to run bellow command for generate new controller

php artisan make:controller SearchController

So now this step, this controller will manage layout post search request,bellow content in controller file. just following the file path

following path:/app/Http/Controllers/SearchController.php

<?php

namespace App\Http\Controllers;

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

class SearchController extends Controller
{
    public function search(Request $request)
    {
    	if($request->has('search')){
    		$posts = Post::search($request->get('search'))->get();	
    	}else{
    		$posts = Post::get();
    	}
        return view('index', compact('posts'));
    }
}
Step 9: Create a View File

Now In this step, let’s create index.blade.php(resources/views/index.blade.php) for layout and then we will write design code here and also post search and recored table, So just need to put following code:

following path:resources/views/index.blade.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Laravel Scout Search Tutorial-codingspoint.com </title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">    
</head>
<body>
    <div class="container">
        <h1>Laravel Scout Search Tutorial-codingspoint.com</h1>
        <div class="row">
            <div class="col-md-6">
                <form method="GET" action="{{ url('index') }}">
                    <div class="row">
                        <div class="col-md-10">
                            <input type="text" name="search" class="form-control" placeholder="Search">
                        </div>
                        <div class="col-md-2">
                            <button class="btn btn-info">Search</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="col-md-4 ">
                <a href="{{ url('index') }}" class="btn btn-danger">Cancel</a>
            </div>
        </div>
   <br/>
      <table class="table table-bordered">
            <tr>
                <th>Id</th>
                <th>Title</th>
                <th>Body</th>
                <th>Category</th>
                <th>View</th>
            </tr>
            @if(count($posts) > 0)
                @foreach($posts as $post)
                <tr>
                    <td>{{ $post->id }}</td>
                    <td>{{ $post->title }}</td>
                    <td>{{ $post->body }}</td>
                    <td>{{ $post->category }}</td>
                    <td>{{ $post->view }}</td>
                </tr>
                @endforeach
            @else
            <tr>
                <td colspan="5" class="text-danger">Result not found.</td>
            </tr>
            @endif
        </table>
   </div>
</body>
</html>

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

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/index

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also 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 →