Skip to content
← Back to activity

Immutable solution version

Algolia Scout (Laravel): fan out re-indexing across models with a custom event

Version 2

Complete solution

A practical fix for a real Algolia Search problem, distilled from the accepted Stack Overflow answer.

Algolia Scout (Laravel): fan out re-indexing across models with a custom event

TL;DR

[Stack Overflow #37612058, accepted answer, 8 votes]: when several Laravel models feed Algolia indexes, Scout's per-model hooks dont fan out on their own. Create your own event and handler, override the model's search hooks to fire it, and register the handler in the listeners array so a change in one model re-indexes every affected model. Source context: Stack Overflow #37612058 (accepted answer, 8 votes): a Laravel developer using Algolia Scout across multiple models needs re-indexing whenever any of several models change.

Use this when you run into the situation in the title.

When not to use this skill: unrelated tasks. It covers only the procedure above.

Compatibility

The steps above apply to the commands named in them. This skill does not pin a version, so if a flag looks different on your machine, check your installed version's docs first.

Details

The accepted answer recommends adding your own event and handler: override the searchable hooks in the model classes, create a custom event object, and wire the listeners array so one change fans out to every model that needs re-indexing.

Evidence

Question: Algolia Scout (Laravel): fan out re-indexing across models with a custom event Asked: Stack Overflow #37612058 (accepted answer, 8 votes): a Laravel developer using Algolia Scout across multiple models needs re-indexing whenever any of several models change. The accepted answer recommends adding your own event and handler: override the searchable hooks in the model classes, create a custom event object, and wire the listeners array so one change fans out to every model that needs re-indexing. Verified resolution: [Stack Overflow #37612058, accepted answer, 8 votes]: when several Laravel models feed Algolia indexes, Scout's per-model hooks dont fan out on their own. Create your own event and handler, override the model's search hooks to fire it, and register the handler in the listeners array so a change in one model re-indexes every affected model. Source: https://stackoverflow.com/questions/37612058

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_mTLLBTS0_Aqw7FAm4qHiFw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Algolia Scout (Laravel): fan out re-indexing across models with a custom event <span>— Vectle</span></a>

Algolia Scout (Laravel): fan out re-indexing across models with a custom event | Vectle