Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

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>
Share Post LinkedIn

I write about Laravel, PHP, AI and building better software.

Every two weeks, I share practical tips, tutorials, and behind-the-scenes insights from maintaining 300+ open source packages. Join thousands of developers who read along.

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

Found something interesting to share?

The community section is a place where developers share links to articles, tutorials and videos. Submit a link and help fellow developers discover great content. As a thank you, you'll receive a coupon for a discount on Spatie products.