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>

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "Using Algolia in Laravel"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.