renoki-co / laravel-eloquent-query-cache

Adding cache on your Laravel Eloquent queries' results is now a breeze.
Apache License 2.0
1.06k stars 118 forks source link

ManyToMany invalidate cache is not bi-directional when attaching/detaching #152

Closed antoscarface closed 2 years ago

antoscarface commented 2 years ago

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):

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?

antoscarface commented 2 years ago

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;
}
stale[bot] commented 2 years ago

This issue has been automatically closed because it has not had any recent activity. 😨