0

I have implemented Aloglia for my Movies table with actors as relational table and it works fine.

Problem:

When I update any movie its also updating algolia index (its good). But how can I update index if I made any change in relational table (for example update an actor of movie).

How to push a specific record manually with laravel scout.

Thanks

Nomi
  • 710
  • 3
  • 11
  • 21

1 Answers1

1

The issue itself lies in laravel's events. Whats happening is scout is listening for an 'updated' event which only occurs in laravel when the model object is saved and is dirty (aka value differ from that in the db).

There are two ways you can do this.

The bad lazy way would simply be to add ->touch() to the model prior to save - this will force the updated_at field to update and ultimately trigger the updated event. This is bad as you're wasting a DB query.

The second and preferable way is to register another observer on 'saved' which triggers regardless of whether or not the object is dirty. Likely you either want to check if the model is dirty and only index when its not (to prevent double indexing from the updated event) or just de-register the 'updated' listener that comes in Scout.

Evan
  • 1,316
  • 2
  • 14
  • 19