Skip to main content
This tutorial targets Laravel 10 or later, with Vite and Vue 3.
While Scout seamlessly wraps the Algolia engine, the Vue.js bundle simplifies the use of Algolia’s InstantSearch library. For this tutorial, you need a fresh Laravel application. You’ll index a Contact model, which holds a name, an email, a company, and a state. The tutorial consists of 4 steps:
  1. Create the model class and seed the database
  2. Install Scout Extended, then index your data
  3. Install Vue InstantSearch
  4. Build your search experience

Create the model

Create an empty model and a migration file with the corresponding artisan command.
Command line
This creates a model class in app/Models/Contact.php and a migration in database/migrations/DATE_create_contacts_table.php. Edit this migration file so it looks like this:
PHP
Now that your Laravel app knows what a contact is, you can migrate the database to reflect these changes.
Command line

Import your fake data

Now you need to import data from Algolia’s contacts dataset and place it at the root of your resources folder. Create a seeder class so that you can select what data to import.
Command line
Laravel generated a new database/seeders/ContactSeeder.php file. Open it up and edit it to add the following class.
PHP
This class opens the JSON dataset and pushes what you need into the database. You can now seed your database:
Command line

Install Scout Extended

Install Scout Extended with Composer. It automatically pulls in Laravel Scout and Algolia’s PHP API client as dependencies, so this is the only package you need to require.
Command line
Thanks to Laravel’s package auto-discovery, the service provider is registered for you. Create a scout.php file in your config folder with the following command:
Command line

Configure Algolia’s credentials

Go to the Algolia dashboard, to the API keys section. You can find three important credentials there:
  • Your application ID
  • Your search-only API key
  • Your admin API key
You need to put these in your .env file. Don’t put these credentials directly in your config/scout.php file, they shouldn’t be part of your version history. Scout defaults to the collection driver, so you also need to set SCOUT_DRIVER to algolia.
.env
The VITE_ variables are exposed to your frontend JavaScript code later in this tutorial. Only use the search-only API key in the frontend, never the admin key.

Index your data

Laravel Scout provides a convenient way to index your data: all you need to do is add a trait to your model. This trait adds functionalities to the default Model class, including keeping your data in sync with Algolia. Open up your app/Models/Contact.php file and add the Laravel\Scout\Searchable trait.
PHP
Before you can keep your data in sync, you need to perform a batch import the first time. Scout has added a command for this.
Command line
Scout Extended’s scout:import command flushes the index before importing, so any existing records in the index are removed first.
Take a look at your dashboard and you should be able to see your data.

Setup Vue.js

You should use frontend search, which gives your users instant results, and also frees up your server from processing every search request. You can use Vue InstantSearch to create a frontend search without writing a lot of custom JavaScript code to handle complex search behaviors.

Install Vue InstantSearch

Laravel uses Vite to bundle frontend assets, but doesn’t ship with Vue by default. Install Vue 3, the Vite plugin for Vue, and the search dependencies with npm.
Command line
Register the Vue plugin in your vite.config.js file:
vite.config.js
Then, open up your resources/js/app.js file, create the Vue app, and register Vue InstantSearch as a plugin.
JavaScript

Build your search experience

Compile JavaScript code

To compile your JavaScript code and rebuild it every time you change one of your files, start the Vite development server:
Command line

Defining where Vue renders

The app.js file from the previous step mounts Vue inside an #app tag.
HTML
Create a new file in your resources/js/components directory called MySearch.vue and register it in app.js for the search interface:
JavaScript
Then update the blade view, adding the my-search component and the @vite directive:
Blade
You can use Vue Devtools to debug your Vue components.
To use InstantSearch’s default theming, import it in resources/css/app.css.
CSS

InstantSearch components

Every InstantSearch component needs to be inside an <ais-instant-search> tag. This lets the library know how to contact Algolia servers. You can set this up in MySearch.vue, using the VITE_ environment variables you added to your .env file earlier.
Vue
To generate a search key that’s restricted to a single model instead, use the Algolia::searchKey() method. For more information, see Client-side search.
The library provides ready-to-use components to add a search box and display the results.
Vue
By default, the component just shows the ID of the results. You can pass a template to change how your results display.
Vue

See also

Last modified on July 23, 2026