The laravel-google-custom-search-engine package is a simple and powerful way to integrate Google Custom Search into your Laravel app. With this package, you can easily search through your website's content, as well as the web at large, using Google's powerful search engine.

Installing and Configuring the Package

To install the package, you can use the following command:

composer require jan-drda/laravel-google-custom-search-engine

Once the package is installed, you will need to add the service provider to your config/app.php file.

PHP

'providers' => [
    ...
    'JanDrda\LaravelGoogleCustomSearchEngine\LaravelGoogleCustomSearchEngineProvider'
];

 

You will also need to add an alias for the GoogleCseSearch facade to your config/app.php file.

PHP

'aliases' => [
    ...
    'GoogleCseSearch' => 'JanDrda\LaravelGoogleCustomSearchEngine\Facades\LaravelGoogleCustomSearchEngineProvider',
];

 

Finally, you will need to publish the config file. This will create a file called google-custom-search-engine.php in your config directory.

php artisan vendor:publish --provider="JanDrda\LaravelGoogleCustomSearchEngine\LaravelGoogleCustomSearchEngineProvider"

The google-custom-search-engine.php config file contains the following options:

  • search_engine_id: The search engine ID for your Google Custom Search Engine.
  • api_key: The API key for your Google Custom Search Engine.
  • language: The language for your Google Custom Search Engine.

You can configure these options to match your specific needs.

Using the Package

Once the package is installed and configured, you can start using it to search for content in your Laravel app. You can use the LaravelGoogleCustomSearchEngine::search() method to search for content. This method takes a search term as its argument and returns an array of results. Each result contains the following information:

  • The title of the result
  • The URL of the result
  • The snippet of the result

You can then use this information to display the search results to your users.

Here is an example of how to use the LaravelGoogleCustomSearchEngine::search() method:

PHP

$results = LaravelGoogleCustomSearchEngine::search('Laravel');

foreach ($results as $result) {
    echo $result->title;
    echo $result->url;
    echo $result->snippet;
}

Conclusion

The laravel-google-custom-search-engine package is a powerful and easy-to-use way to integrate Google Custom Search into your Laravel app. If you are looking for a way to make it easy for your users to find content on your website, then this package is a great option.

I hope this article has been helpful!