/** * Boot the countable behaviour and setup the appropriate event bindings. */ public static function bootCountable() { static::created(function ($model) { $countCache = new CountCache($model); $countCache->apply(function ($config) use($countCache, $model) { $countCache->updateCacheRecord($config, '+', 1, $model->{$config['foreignKey']}); }); }); static::updated(function ($model) { (new CountCache($model))->update(); }); static::deleted(function ($model) { $countCache = new CountCache($model); $countCache->apply(function ($config) use($countCache, $model) { if (isset($config['polymorphic'])) { $relations = $model->{strtolower($config['model'])}; if ($relations) { foreach ($relations as $relation) { $countCache->updateCacheRecord($config, '-', 1, $relation->id); } } } $countCache->updateCacheRecord($config, '-', 1, $model->{$config['foreignKey']}); }); }); }
/** * Boot the countable behaviour and setup the appropriate event bindings. */ public static function bootCountable() { static::created(function ($model) { $countCache = new CountCache($model); $countCache->apply(function ($config) use($countCache, $model) { $countCache->updateCacheRecord($config, '+', 1, $model->{$config['foreignKey']}); }); }); static::updated(function ($model) { (new CountCache($model))->update(); }); static::deleted(function ($model) { $countCache = new CountCache($model); $countCache->apply(function ($config) use($countCache, $model) { $countCache->updateCacheRecord($config, '-', 1, $model->{$config['foreignKey']}); }); }); }