
Published on: September 24, 2020
In today's internet world, a robust search engine is an indispensable tool. This article focuses on the creation search engine on Laravel sites using Solr, demonstrating how easily Solr can be integrated to power database searches within your Laravel applications. We'll guide you through implementing Solr and its PHP bridge, the Solarium library, within the Laravel framework.
Solr configuration
First, configure Solr by setting up `config/solr.php`:
[
'localhost' => [
'host' => env('SOLR_HOST', '192.168.99.100'),
'port' => env('SOLR_PORT', '8983'),
#'path' => env('SOLR_PATH', '/solr/'),
'core' => env('SOLR_CORE', 'mycore')
]
]
];
Install the PHP Solarium library
To facilitate the creation search engine on Laravel sites using Solr, you'll need the Solarium library. Install it via Composer:
composer require solarium/solarium
Boot up the Solarium Client
Next, create a service provider to boot the Solarium Client:
php artisan make:provider SolariumServiceProvider
Edit `app/Providers/SolariumServiceProvider.php` to bind the Solarium Client:
app->bind(Client::class, function ($app) {
return new Client($app['config']['solr']);
});
}
public function provides()
{
return [Client::class];
}
}
Now, open `app/config/app.php` and add the `SolariumServiceProvider` to your application providers:
return [ 'providers' => [ // List off others providers... App\Providers\SolariumServiceProvider::class, ] ];
The integration
For the creation search engine on Laravel sites using Solr, you'll integrate the search functionality through a controller. Create a new controller:
php artisan make:controller SolariumController
Add the search logic to `app/Http/Controllers/SolariumController.php`:
client = $client;
}
public function search()
{
$input = Input::get('q');
$query = $this->client->createSelect();
$query->setQuery('*:*');
$query->setQuery('name:"' . $input . '"');
$query->setStart(0);
$query->setRows(10);
$resultset = $this->client->select($query);
return view('search', compact('resultset'));
}
}
Add the search results view to `resources/views/search.blade.php`:
@if (count($resultset) > 0)
@foreach ($resultset as $data)
{{ $data->name }}
@endforeach
@else
Data Not Found.
@endif
Finally, add the search route to your `app/Http/routes.php` file:
Route::get('/search', 'SolariumController@search');
Related Posts

Importing MySQL Databases Using Solr DataImportHandler
Streamline MySQL database imports with Solr DataImportHandler! This guide covers setup, config, and execution for efficient data indexing.
Read More
React Native CLI vs. Expo: Battle of Mobile Devs
Dive deep into the core differences between React Native CLI and Expo for mobile development. Discover which tool fits your project and workflow best.
Read More
Tailwind CSS to React Native, NativeWind: a Savior
Boost your React Native app's performance. Convert Tailwind to StyleSheet with NativeWind for faster development and incredible results.
Read More