7

When I import the index everything works fine (including relations).

The problem is that the main model does not watch over relations.

When I update a relation, the index is not updated too.

Is there any way to use something similar with Cache::tags to update the index when a relation is modified? Or maybe is another way.

techraf
  • 64,883
  • 27
  • 193
  • 198
Dorin Niscu
  • 721
  • 1
  • 9
  • 26

1 Answers1

3

There is, unfortunately, no direct way to do this using Scout. However, using another Laravel feature you can 'trick' Laravel into updating the record.

Add a $touches variable to each of the child classes containing the method names of the relationship to the parent. For example, if you had a Comment class with a post() method returning the belongsTo() relationship, you'd add to the Comment class:

protected $touches = ['post'];

When a comment is modified, it will update the updated_at field of the parent, which Scout will see and update the record.

Freddy Heppell
  • 276
  • 3
  • 17
  • 1
    This might not work for `*Many` relationships, e.g. `belongsToMany`. There was an attempt to fix this for 5.1, but this still seems to be an issue as of 5.6: https://github.com/laravel/framework/pull/19275 – Illya Moskvin May 16 '18 at 15:41