/** * Rebuilds the caches for the given class. * * @param string $className */ private function rebuild($className) { $instance = new $className(); if (method_exists($instance, 'countCaches')) { $this->info("Rebuilding [{$className}] count caches"); $countCache = new CountCache($instance); $countCache->rebuild(); } if (method_exists($instance, 'sumCaches')) { $this->info("Rebuilding [{$className}] sum caches"); $sumCache = new SumCache($instance); $sumCache->rebuild(); } }
/** * Boot the trait and its event bindings when a model is created. */ public static function bootSummable() { static::created(function ($model) { $sumCache = new SumCache($model); $sumCache->apply(function ($config) use($model, $sumCache) { $sumCache->updateCacheRecord($config, '+', $model->{$config['columnToSum']}, $model->{$config['foreignKey']}); }); }); static::updated(function ($model) { (new SumCache($model))->update(); }); static::deleted(function ($model) { $sumCache = new SumCache($model); $sumCache->apply(function ($config) use($model, $sumCache) { $sumCache->updateCacheRecord($config, '-', $model->{$config['columnToSum']}, $model->{$config['foreignKey']}); }); }); }