From f03209acbf3c391388a5327ed1389e12f62b7c3e Mon Sep 17 00:00:00 2001 From: tabuna Date: Fri, 18 Feb 2022 02:16:42 +0300 Subject: [PATCH] ## 3.6.0 - 2022-02-18 --- CHANGELOG.md | 6 ++++ src/CrudScreen.php | 8 ++--- src/Resource.php | 58 +++++++++++++++++++++++++----------- src/Screens/CreateScreen.php | 2 +- 4 files changed, 52 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8055334..fb4a86d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to `crud` will be documented in this file +## 3.6.0 - 2022-02-18 + +### Changed + +- Passing model type in default actions + ## 3.5.0 - 2022-02-10 ### Changed diff --git a/src/CrudScreen.php b/src/CrudScreen.php index 411e3b5..16585f3 100644 --- a/src/CrudScreen.php +++ b/src/CrudScreen.php @@ -160,7 +160,7 @@ public function action(ActionRequest $request) */ public function update(UpdateRequest $request) { - $request->resource()->onSave($request, $request->findModelOrFail()); + $request->resource()->save($request, $request->findModelOrFail()); Toast::info($this->resource::updateToastMessage()); @@ -176,7 +176,7 @@ public function update(UpdateRequest $request) */ public function delete(DeleteRequest $request) { - $request->resource()->onDelete( + $request->resource()->delete( $request->findModelOrFail() ); @@ -194,7 +194,7 @@ public function delete(DeleteRequest $request) */ public function forceDelete(ForceDeleteRequest $request) { - $request->resource()->onForceDelete( + $request->resource()->forceDelete( $request->findModelOrFail() ); @@ -210,7 +210,7 @@ public function forceDelete(ForceDeleteRequest $request) */ public function restore(RestoreRequest $request) { - $request->resource()->onRestore( + $request->resource()->restore( $request->findModelOrFail() ); diff --git a/src/Resource.php b/src/Resource.php index b35adf9..6c16766 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -386,23 +386,40 @@ public function modelQuery(ResourceRequest $request, Model $model): Builder /** * Action to query models list * - * @param Model $model + * @param \Orchid\Crud\ResourceRequest $request + * @param Model $model * - * @return Model $model + * @return \Illuminate\Database\Eloquent\Builder $model */ public function paginationQuery(ResourceRequest $request, Model $model): Builder { return $model->query(); } + /** + * Determine if this resource uses soft deletes. + * + * @return bool + */ + public static function softDeletes(): bool + { + return in_array(SoftDeletes::class, class_uses_recursive(static::$model), true); + } + /** * Action to create and update the model * * @param ResourceRequest $request * @param Model $model */ - public function onSave(ResourceRequest $request, Model $model) + public function save(ResourceRequest $request, Model $model): void { + if (method_exists(static::class, 'onSave')) { + static::onSave($request, $model); + + return; + } + $model->forceFill($request->all())->save(); } @@ -413,19 +430,15 @@ public function onSave(ResourceRequest $request, Model $model) * * @throws Exception */ - public function onDelete(Model $model) + public function delete(Model $model): void { - $model->delete(); - } + if (method_exists(static::class, 'onDelete')) { + static::onDelete($model); - /** - * Determine if this resource uses soft deletes. - * - * @return bool - */ - public static function softDeletes(): bool - { - return in_array(SoftDeletes::class, class_uses_recursive(static::$model), true); + return; + } + + $model->delete(); } /** @@ -433,8 +446,14 @@ public static function softDeletes(): bool * * @param Model $model */ - public function onRestore(Model $model) + public function restore(Model $model): void { + if (method_exists(static::class, 'onRestore')) { + static::onRestore($model); + + return; + } + $model->restore(); } @@ -445,9 +464,14 @@ public function onRestore(Model $model) * * @throws Exception */ - public function onForceDelete(Model $model) + public function forceDelete(Model $model): void { - // Force deleting a single model instance... + if (method_exists(self::class, 'onForceDelete')) { + static::onForceDelete($model); + + return; + } + $model->forceDelete(); } } diff --git a/src/Screens/CreateScreen.php b/src/Screens/CreateScreen.php index a0fad0d..56e7106 100644 --- a/src/Screens/CreateScreen.php +++ b/src/Screens/CreateScreen.php @@ -61,7 +61,7 @@ public function save(CreateRequest $request) { $model = $request->model(); - $request->resource()->onSave($request, $model); + $request->resource()->save($request, $model); Toast::info($this->resource::createToastMessage());