From 8911c538957b7ea8b8779c4cbd3c6b27a2066ffb Mon Sep 17 00:00:00 2001 From: Josh Taylor Date: Tue, 26 Jul 2016 17:21:55 +0100 Subject: [PATCH] Before & After commit hooks Adds callbacks for creating/created/updating/updated models on a controller. Callbacks can be defined in controllers that inherit the `EloquentController`. ```php class UsersController extends EloquentController { protected function creating(Model $model) { // Before the model is created. } protected function created(Model $model) { // After the model has been created. } protected function updating(Model $model) { // Before the model is updated. } protected function updated() { // After the model has been created. } } ``` --- See #11 for a bit more discussion.` --- src/Http/Controllers/EloquentController.php | 86 ++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/src/Http/Controllers/EloquentController.php b/src/Http/Controllers/EloquentController.php index 19f77722..af2fde45 100644 --- a/src/Http/Controllers/EloquentController.php +++ b/src/Http/Controllers/EloquentController.php @@ -244,7 +244,47 @@ protected function hydrate(ResourceInterface $resource, Model $model) */ protected function commit(Model $model) { - return $model->save(); + $isUpdating = $model->exists; + + $this->beforeCommit($model, $isUpdating); + + $result = $model->save(); + + if ($result) { + $this->afterCommit($model, $isUpdating); + } + + return $result; + } + + /** + * Determines which callback to use before creating or updating a model. + * + * @param Model $model + * @param bool $isUpdating + */ + protected function beforeCommit(Model $model, $isUpdating) + { + if ($isUpdating) { + $this->updating($model); + } else { + $this->creating($model); + } + } + + /** + * Determines which callback to use after a model is updated or created. + * + * @param Model $model + * @param bool $isUpdating + */ + protected function afterCommit(Model $model, $isUpdating) + { + if ($isUpdating) { + $this->updated($model); + } else { + $this->created($model); + } } /** @@ -350,4 +390,48 @@ private function doDestroy(Model $model) return $this->destroy($model); }); } + + /** + * Called before the model is created. + * + * Child classes can overload this method if they need to do any logic pre-creation. + * + * @param Model $model + */ + protected function creating(Model $model) + { + } + + /** + * Called after the model has been created. + * + * Child classes can overload this method if they need to do any logic post-creation. + * + * @param Model $model + */ + protected function created(Model $model) + { + } + + /** + * Called before the model is updated. + * + * Child classes can overload this method if they need to do any logic pre-updating. + * + * @param Model $model + */ + protected function updating(Model $model) + { + } + + /** + * Called after the model has been updated. + * + * Child classes can overload this method if they need to do any logic post-updating. + * + * @param Model $model + */ + protected function updated(Model $model) + { + } }