Laravel Scout is a package that provides a simple, driver-based solution for adding full-text search to your Eloquent models. To use Laravel Scout for full-text search in a ReactJS application, you'll need to follow these general steps:
Install Scout:
bashcomposer require laravel/scout
Publish the Configuration:
bashphp artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
Choose and Configure a Search Driver:
Laravel Scout supports multiple search engines, such as Algolia, Elasticsearch, and Meilisearch. Choose the one that fits your requirements and configure it in the config/scout.php
file.
Index Your Models:
In your Eloquent model, implement the Searchable
trait.
phpuse Laravel\Scout\Searchable;
class YourModel extends Model
{
use Searchable;
}
Run php artisan scout:import "App\Models\YourModel"
to index your existing records.
Create API endpoints in your Laravel application to handle search requests.
php// routes/api.php
Route::get('/search', 'SearchController@search');
Create a SearchController
with a search
method.
php// app/Http/Controllers/SearchController.php
use App\Models\YourModel;
use Illuminate\Http\Request;
class SearchController extends Controller
{
public function search(Request $request)
{
$query = $request->input('query');
$results = YourModel::search($query)->get();
return response()->json($results);
}
}
Set Up Your React App: Set up a ReactJS application using Create React App or your preferred method.
Create a Search Component: Create a component for your search functionality. This component will interact with the Laravel API endpoint.
jsx// src/components/Search.js
import React, { useState } from 'react';
const Search = () => {
const [query, setQuery] = useState('');
const [results, setResults] = useState([]);
const handleSearch = async () => {
try {
const response = await fetch(`/api/search?query=${query}`);
const data = await response.json();
setResults(data);
} catch (error) {
console.error('Error fetching search results:', error);
}
};
return (
<div>
<input
type="text"
placeholder="Search..."
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
<button onClick={handleSearch}>Search</button>
<ul>
{results.map((result) => (
<li key={result.id}>{result.name}</li>
// Adjust the properties based on your model
))}
</ul>
</div>
);
};
export default Search;
Search
component into your main application.jsx// src/App.js
import React from 'react';
import Search from './components/Search';
const App = () => {
return (
<div>
<h1>React Laravel Scout Search</h1>
<Search />
</div>
);
};
export default App;
Run Laravel: Start your Laravel development server:
bashphp artisan serve
Run React App: Start your React development server:
bashnpm start
Visit http://localhost:3000
(or the URL specified by your React app) to see your React application with the Laravel Scout full-text search functionality.
Remember to adjust the code based on your specific models and requirements. The example assumes a basic setup, and you may need to customize it according to your application structure.