This tutorial targets Laravel 10 or later, with Vite and Vue 3.
Contact model, which holds a name, an email, a company, and a state.
The tutorial consists of 4 steps:
- Create the model class and seed the database
- Install Scout Extended, then index your data
- Install Vue InstantSearch
- Build your search experience
Create the model
Create an empty model and a migration file with the correspondingartisan command.
Command line
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
Command line
Import your fake data
Now you need to import data from Algolia’s contacts dataset and place it at the root of yourresources folder.
Create a seeder class so that you can select what data to import.
Command line
database/seeders/ContactSeeder.php file. Open it up and edit it to add the following class.
PHP
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
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
.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
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 defaultModel 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
Command line
Scout Extended’s
scout:import command flushes the index before importing, so any existing records in the index are removed first.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
vite.config.js file:
vite.config.js
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
Theapp.js file from the previous step mounts Vue inside an #app tag.
HTML
resources/js/components directory called MySearch.vue and register it in app.js for the search interface:
JavaScript
my-search component and the @vite directive:
Blade
You can use Vue Devtools to debug your Vue components.
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.Vue
Vue