This tutorial was written for Laravel 8.
It doesn’t work with newer versions of Laravel.
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, 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/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
PHP
Install Laravel Scout
Install Laravel Scout and Algolia’s API client using composer.Command line
providers array in the config/app.php file.
If you use Laravel 5.5 and later, you can skip this step.
PHP
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.
.env
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/Contact.php file and add the Laravel/Scout/Searchable trait.
PHP
Command line
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
Since Laravel ships with Vue by default, you have nothing to setup. You’ll only need to addvue-instantsearch as a dependency with npm and register it as a Vue plugin.
Command line
resources/js/app.js and add the following line just after the window.Vue = require('vue'); line.
Build your search experience
Compile JavaScript code
To compile the JavaScript into theapp.js bundle file,
run the following command:
Command line
Defining where Vue renders
In a default Laravel app, Vue renders inside an#app tag.
HTML
components directory called MySearch.vue and register it in app.js for the search interface:
JavaScript
my-search component:
PHP
You can use
vue-devtools to debug your Vue components.scss/app.scss.
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.
Vue
Vue
Vue