diff --git a/src/Database/Model.php b/src/Database/Model.php index 5e701aa2e..556171190 100644 --- a/src/Database/Model.php +++ b/src/Database/Model.php @@ -226,11 +226,14 @@ protected function bootNicerEvents() } self::$eventMethod(function ($model) use ($method) { - $model->fireEvent('model.' . $method); + $result = null; if ($model->methodExists($method)) { - return $model->$method(); + $result = $model->$method(); } + $model->fireEvent('model.' . $method); + + return $result; }); } } diff --git a/src/Database/Traits/SoftDelete.php b/src/Database/Traits/SoftDelete.php index 1b7243514..4c2320018 100644 --- a/src/Database/Traits/SoftDelete.php +++ b/src/Database/Traits/SoftDelete.php @@ -24,6 +24,9 @@ public static function bootSoftDelete() static::addGlobalScope(new SoftDeletingScope); static::restoring(function ($model) { + if ($model->methodExists('beforeRestore')) { + $model->beforeRestore(); + } /** * @event model.beforeRestore * Called before the model is restored from a soft delete @@ -36,12 +39,12 @@ public static function bootSoftDelete() * */ $model->fireEvent('model.beforeRestore'); - if ($model->methodExists('beforeRestore')) { - $model->beforeRestore(); - } }); static::restored(function ($model) { + if ($model->methodExists('afterRestore')) { + $model->afterRestore(); + } /** * @event model.afterRestore * Called after the model is restored from a soft delete @@ -54,9 +57,6 @@ public static function bootSoftDelete() * */ $model->fireEvent('model.afterRestore'); - if ($model->methodExists('afterRestore')) { - $model->afterRestore(); - } }); } diff --git a/src/Database/Traits/Validation.php b/src/Database/Traits/Validation.php index a4f68504b..e7e6f59ea 100644 --- a/src/Database/Traits/Validation.php +++ b/src/Database/Traits/Validation.php @@ -174,6 +174,10 @@ public function validate($rules = null, $customMessages = null, $attributeNames ? $this->throwOnValidation : true; + if ($this->methodExists('beforeValidate')) { + $this->beforeValidate(); + } + /** * @event model.beforeValidate * Called before the model is validated @@ -194,10 +198,6 @@ public function validate($rules = null, $customMessages = null, $attributeNames return false; } - if ($this->methodExists('beforeValidate')) { - $this->beforeValidate(); - } - /* * Perform validation */ @@ -323,6 +323,10 @@ public function validate($rules = null, $customMessages = null, $attributeNames } } + if ($this->methodExists('afterValidate')) { + $this->afterValidate(); + } + /** * @event model.afterValidate * Called after the model is validated @@ -337,10 +341,6 @@ public function validate($rules = null, $customMessages = null, $attributeNames $this->fireModelEvent('validated', false); $this->fireEvent('model.afterValidate'); - if ($this->methodExists('afterValidate')) { - $this->afterValidate(); - } - if (!$success && $throwOnValidation) { throw new ModelException($this); } diff --git a/src/Halcyon/Model.php b/src/Halcyon/Model.php index 65ca0ee4e..c796ebf23 100644 --- a/src/Halcyon/Model.php +++ b/src/Halcyon/Model.php @@ -239,11 +239,14 @@ protected function bootNicerEvents() } self::$eventMethod(function ($model) use ($method) { - $model->fireEvent('model.' . $method); + $result = null; if ($model->methodExists($method)) { - return $model->$method(); + $result = $model->$method(); } + $model->fireEvent('model.' . $method); + + return $result; }); } } diff --git a/src/Halcyon/Traits/Validation.php b/src/Halcyon/Traits/Validation.php index 0c6a1950d..6861d8997 100644 --- a/src/Halcyon/Traits/Validation.php +++ b/src/Halcyon/Traits/Validation.php @@ -120,6 +120,10 @@ public function validate($rules = null, $customMessages = null, $attributeNames ? $this->throwOnValidation : true; + if ($this->methodExists('beforeValidate')) { + $this->beforeValidate(); + } + if (($this->fireModelEvent('validating') === false) || ($this->fireEvent('model.beforeValidate') === false)) { if ($throwOnValidation) { throw new ModelException($this); @@ -128,10 +132,6 @@ public function validate($rules = null, $customMessages = null, $attributeNames return false; } - if ($this->methodExists('beforeValidate')) { - $this->beforeValidate(); - } - /* * Perform validation */ @@ -209,13 +209,13 @@ public function validate($rules = null, $customMessages = null, $attributeNames } } - $this->fireModelEvent('validated', false); - $this->fireEvent('model.afterValidate'); - if ($this->methodExists('afterValidate')) { $this->afterValidate(); } + $this->fireModelEvent('validated', false); + $this->fireEvent('model.afterValidate'); + if (!$success && $throwOnValidation) { throw new ModelException($this); }