Laravel Scout is a powerful package that provides a simple, driver-based solution for adding full-text search to your Laravel models. To integrate Laravel Scout with a Vue.js application for full-text search, you can follow these general steps:
Install Laravel Scout: First, make sure you have Laravel Scout installed. You can install it via Composer:
bashcomposer require laravel/scout
Then, publish the Scout configuration file:
bashphp artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
Choose a Scout Driver: Laravel Scout supports various search engines like Algolia, Elasticsearch, and Meilisearch. Choose a search engine that fits your requirements. For this example, let's use Algolia.
Install the Algolia Scout driver:
bashcomposer require algolia/algoliasearch-client-php composer require laravel/scout-extended
Configure your .env
file with your Algolia credentials:
dotenvALGOLIA_APP_ID=your-app-id ALGOLIA_SECRET=your-api-key
Update your config/scout.php
file to use the Algolia driver:
php'driver' => env('SCOUT_DRIVER', 'algolia'),
Index Your Models:
Make sure the models you want to search are searchable. Implement the Searchable
trait in your models:
phpuse Laravel\Scout\Searchable;
class YourModel extends Model
{
use Searchable;
}
After updating your model, run:
bashphp artisan scout:import "App\YourModel"
Set Up the Vue.js Component: Create a Vue.js component that will handle the search functionality. This component should communicate with your Laravel backend to perform the search.
Here's a simple example using Axios for HTTP requests:
javascript<template>
<div>
<input v-model="query" @input="search" placeholder="Search...">
<ul>
<li v-for="result in results" :key="result.id">
{{ result.name }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
query: '',
results: [],
};
},
methods: {
search() {
axios.get(`/api/search?q=${this.query}`)
.then(response => {
this.results = response.data;
})
.catch(error => {
console.error('Error performing search', error);
});
},
},
};
</script>
Create a Laravel Controller for Search: Create a controller to handle the search requests and return the results:
phpnamespace App\Http\Controllers;
use App\YourModel;
use Illuminate\Http\Request;
class SearchController extends Controller
{
public function search(Request $request)
{
$query = $request->input('q');
$results = YourModel::search($query)->get();
return response()->json($results);
}
}
Define the Route:
Finally, define a route in your routes/web.php
file or routes/api.php
if it's an API endpoint:
phpRoute::get('/api/search', 'SearchController@search');
Use the Vue.js Component: Include your Vue.js component in your Blade file and use it where you need search functionality.
html<div id="app">
<search-component></search-component>
</div>
<script src="{{ mix('js/app.js') }}"></script>
Make sure to compile your assets with Laravel Mix:
bashnpm run dev
Now, your Laravel application should be set up for full-text search using Laravel Scout with a Vue.js frontend. Adjust the code according to your specific needs and styling preferences.