mirror of
https://github.com/flarum/core.git
synced 2025-07-24 02:01:19 +02:00
Add API to run callback after a model instance is saved
This commit is contained in:
@@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
- `route` attribute for Mithril elements as a shortcut to link to a route.
|
- `route` attribute for Mithril elements as a shortcut to link to a route.
|
||||||
- Abstract SettingsModal component for quickly building admin config modals.
|
- Abstract SettingsModal component for quickly building admin config modals.
|
||||||
- "Debug" button to inspect the response of a failed AJAX request.
|
- "Debug" button to inspect the response of a failed AJAX request.
|
||||||
|
- `Model::afterSave()` API to run callback after a model instance is saved.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- Migrations must be namespaced under `Flarum\Migrations\{Core|ExtensionName}`. ([#422](https://github.com/flarum/core/issues/422))
|
- Migrations must be namespaced under `Flarum\Migrations\{Core|ExtensionName}`. ([#422](https://github.com/flarum/core/issues/422))
|
||||||
|
@@ -35,6 +35,29 @@ abstract class Model extends Eloquent
|
|||||||
*/
|
*/
|
||||||
public $timestamps = false;
|
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.
|
* 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
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user