Using Algolia in Laravel original

by Freek Van der Herten – 2 minute read

Algolia is a hosted service that makes advanced searching very easy. It's well documented and lightning quick. You can see some impressive examples on their site. Artisans probably know that Jeffrey Way recently published a series on Algolia.

Earlier this year I made a package to easily work with a searchindex in Laravel. Basically it provides:

  • an interface that makes your objects searchable
  • some easy to use functions to put those objects into the search index
  • a function to retrieve search results from the index
The only supported index was Elasticsearch. I've now added support for Algolia to that package. It makes working with Algolia a cinch. Sure, there's the official Laravel integration, and it probably works well, but it felt a bit too "heavy" for my needs.

Using my package you just got to implement the Searchable-interface on an object you want to index. That object can be an Eloquent model, but you really can use it on any class you like. Once the interface is implemented these methods can be used:

[code language="php"] //$product is an object that implements the Searchable interface

//adding or updating something to the index SearchIndex::upsertToIndex($product);

//removing something from the index SearchIndex::removeFromIndex($product);

SearchIndex::getResults('my favorite product');



To perform more advanced queries an array may be passed to the `getResults`-function. Read <a href="https://github.com/algolia/algoliasearch-client-php#search">the official Algolia documentation</a> to learn what's possible.

You'll find some more information on the package on GitHub:
<a href="https://github.com/spatie/searchindex">https://github.com/spatie/searchindex</a>

Join 9,500+ smart developers

Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.

No spam. Unsubscribe anytime. You can also follow me on X.

Found something interesting to share? Submit a link to the community section.