From 87b54071cbcff48b2c9a5cbdacabe2620408e63d Mon Sep 17 00:00:00 2001 From: Alexis Gousseau Date: Wed, 17 Dec 2025 17:14:38 +0100 Subject: [PATCH] fix: :bug: set getCacheKey from cache trait private --- app/Traits/Models/HasCache.php | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/app/Traits/Models/HasCache.php b/app/Traits/Models/HasCache.php index 6a94517a..664622ac 100644 --- a/app/Traits/Models/HasCache.php +++ b/app/Traits/Models/HasCache.php @@ -19,16 +19,6 @@ trait HasCache */ public static function bootHasCache(): void { - $hadSoftDelete = method_exists(static::class, 'initializeSoftDeletes'); - - if ($hadSoftDelete) { - static::restored(function () { - self::resetCache(); - }); - static::forceDeleted(function () { - self::resetCache(); - }); - } static::created(function () { self::resetCache(); }); @@ -38,6 +28,14 @@ trait HasCache static::deleted(function () { self::resetCache(); }); + if (method_exists(static::class, 'initializeSoftDeletes')) { + static::restored(function () { + self::resetCache(); + }); + static::forceDeleted(function () { + self::resetCache(); + }); + } } /** @@ -47,11 +45,7 @@ trait HasCache */ public static function resetCache(): void { - $class = static::class; - $cacheKey = static::getCacheKey(); - - Cache::forget($cacheKey); - + Cache::forget(static::getCacheKey()); static::getModelsCached(); } @@ -62,10 +56,9 @@ trait HasCache */ public static function getModelsCached(): \Illuminate\Database\Eloquent\Collection { - $class = static::class; - $cacheKey = static::getCacheKey(); + return Cache::remember(static::getCacheKey(), 86400, function () { + $class = static::class; - return Cache::remember($cacheKey, 86400, function () use ($class) { /** @var \Illuminate\Database\Eloquent\Model $class */ return $class::query() ->when(property_exists($class, 'cacheRelations') && is_array($class::$cacheRelations), function ($query) use ($class) { @@ -80,7 +73,7 @@ trait HasCache * * @return string */ - protected static function getCacheKey(): string + private static function getCacheKey(): string { return 'cache_' . str_replace('\\', '_', static::class); }