From 5e11231699396130ff5235fe027f01021c3fc957 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 | 84 ++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/src/Http/Controllers/EloquentController.php b/src/Http/Controllers/EloquentController.php index 19f77722..5abfac83 100644 --- a/src/Http/Controllers/EloquentController.php +++ b/src/Http/Controllers/EloquentController.php @@ -244,7 +244,45 @@ 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(); + + $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) { + return $this->updating($model); + } + + return $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) { + return $this->updated($model); + } + + return $this->created($model); } /** @@ -350,4 +388,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) + { + } }