ManyToMany invalidate cache is not bi-directional when attaching/detaching
Hi,
I'm using your library to cache entities with Eloquent and I also read this in order to invalidate cache also with ManyToMany relationships, using pivot tables.
I installed chelout/laravel-relationship-events and added the traits, it works in one direction and not in the other.
I explain with an example:
Given these models:
class ModelA extends Model {
use HasBelongsToManyEvents;
use HasRelationshipObservables;
use QueryCacheable;
protected static bool $flushCacheOnUpdate = true;
public function modelBs(): BelongsToMany
{
return $this->belongsToMany(ModelB::class, 'model_a_b')
->using(ModelABPivot::class);
}
}
...
class ModelB extends Model {
use HasBelongsToManyEvents;
use HasRelationshipObservables;
use QueryCacheable;
protected static bool $flushCacheOnUpdate = true;
public function modelAs(): BelongsToMany
{
return $this->belongsToMany(ModelA::class, 'model_a_b')
->using(ModelABPivot::class);
}
}
...
class ModelABPivot extends Pivot {
}
When I run $modelA->modelBs()->attach($modelB):
$modelB->modelAs()returns the$modelAattached$modelA->modelBs()does not
I do need ModelA::flushCache() to make it work.
Same thing vice-versa when using detach.
What's wrong with what I did? Could you please help me?
It doesn't work even if I add the cache trait in the pivot table class:
class ModelABPivot extends Pivot {
use QueryCacheable;
protected static bool $flushCacheOnUpdate = true;
}
This issue has been automatically closed because it has not had any recent activity. 😨