phpmysqllaravelelasticsearchelasticsearch-indices

Laravel Scout - observe relations


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.


Solution

  • 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.