Laravel Scout - observe relations
Asked Answered
J

1

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.

Jedthus answered 9/10, 2016 at 7:53 Comment(0)
S
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.

Spinach answered 14/3, 2018 at 9:40 Comment(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: github.com/laravel/framework/pull/19275Engedi

© 2022 - 2024 — McMap. All rights reserved.