Ejemplo n.º 1
0
 /**
  * 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']});
         });
     });
 }