1
0
mirror of https://github.com/flarum/core.git synced 2025-08-01 22:20:21 +02:00

Add API to run callback after a model instance is saved

This commit is contained in:
Toby Zerner
2015-09-22 17:22:25 +09:30
parent ed602c6032
commit 8e19312534
2 changed files with 35 additions and 0 deletions

View File

@@ -35,6 +35,29 @@ abstract class Model extends Eloquent
*/
public $timestamps = false;
/**
* An array of callbacks to be run once after the model is saved.
*
* @var callable[]
*/
public $afterSaveCallbacks = [];
/**
* {@inheritdoc}
*/
public static function boot()
{
parent::boot();
static::saved(function (Model $model) {
foreach ($model->afterSaveCallbacks as $callback) {
$callback($model);
}
$model->afterSaveCallbacks = [];
});
}
/**
* Get the attributes that should be converted to dates.
*
@@ -94,6 +117,17 @@ abstract class Model extends Eloquent
);
}
/**
* Register a callback to be run once after the model is saved.
*
* @param callable $callback
* @return void
*/
public function afterSave($callback)
{
$this->afterSaveCallbacks[] = $callback;
}
/**
* @inheritdoc
*/