Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 764
Dorin Niscu Avatar asked Oct 09 '16 07:10

Dorin Niscu


1 Answers

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.

like image 73
Freddy Heppell Avatar answered Sep 28 '22 15:09

Freddy Heppell