Framework integration / Laravel / Getting started

Algolia Scout Extended requires the following PHP and Laravel versions:

Scout Extended version PHP version Laravel version
3 8.1 and later 10 and later
  8.0 and later 9 and later
2 7.3 and later 8 and later
1 7.3 and later 6

For more information, read the Scout Extended changelog.

  1. Install Scout Extended with Composer:

    $
    $
    
     # Install the latest version of Scout Extended
     composer require "algolia/scout-extended:^3.0"
    
  2. Publish the Scout configuration to your config directory:

    $
    
     php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
    
  3. For the model you would like to make searchable, add the Searchable trait. For example, for an Article model (app/Models/Article.php):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
     namespace App\Models;
    
     use Laravel\Scout\Searchable;
     use Illuminate\Database\Eloquent\Model;
    
     class Article extends Model
     {
         use Searchable;
     }
    

    Replace Article with a model used in your app.

  4. While not required, you should use a queue driver for Scout operations. Once you’ve configured a queue driver, set queue to true in your config/scout.php file:

    1
    
     'queue' => true,
    
  5. To access Algolia, copy your API key values from the Algolia dashboard to your .env file:

    $
    $
    
     ALGOLIA_APP_ID=<ALGOLIA_APP_ID>
     ALGOLIA_SECRET=<ALGOLIA_APP_ID>
    
    • <ALGOLIA_APP_ID>. Your Algolia application ID.
    • <ALGOLIA_API_KEY>. Your API key with search and addObject permissions.

    Your Admin API key grants full access to your Algolia application. Don’t share it with anyone: it must remain confidential.

  6. You probably have existing data that you would like to import:

    • If you’re importing from your Laravel database for the first time, use scout:import.
    • If you’ve installed Scout Extended in a Laravel Scout project, use scout:reimport.

    If you make significant changes to your Scout configuration or need to reindex all your data, use scout:reimport to ensure everything is in sync.

  7. Open the routes/web.php file and add the following route to the bottom of the file:

    1
    2
    3
    4
    5
    6
    7
    8
    
     Route::get('search', function() {
         $query = ''; // <-- Change the query for testing.
         // Visit the /search route in your web browser to see articles that match the test $query.
    
         $articles = App\Article::search($query)->get();
    
         return $articles;
     });
    

    Replace Article with a model used in your app.

Did you find this page helpful?