How to add Algolia Search with Laravel Scout

Install package

composer require laravel/scout

Publish Scout configuration

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

Add Laravel\Scout\Searchable trait to your model


namespace App;

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

class Post extends Model
    use Searchable;

Change settings in config/scout.php

'queue' => true,

Instal Algolia

composer require algolia/algoliasearch-client-php:^2.2

Create Account

Add Algolia id and secret in .env


Import your database records

php artisan scout:import "App\Blog\Post"
php artisan scout:flush "App\Post"

Add to your View file view.blade.php

<input type="text" id="search-input" placeholder="Search" />

Add Javascript

<script src=""></script>
<script src=""></script>

  var client = algoliasearch( 'ALGOLIA_APP_ID' , 'ALGOLIA_SEARCH' );

  var index = client.initIndex('posts');
  autocomplete('#search-input', { hint: true }, [
      source: autocomplete.sources.hits(index, { hitsPerPage: 3 }),
      displayKey: 'title',
      templates: {
        suggestion: function(suggestion) {
          return suggestion._highlightResult.title.value;
        footer: '<div class="aa-dropdown-footer">Search by<img class="aa-logo" src="" width=80/></div>'
  .on('autocomplete:selected', function(event, suggestion, dataset, context) {
        window.location = suggestion.slug