From 7cfdd78f668ceb60aeec353ae6245cd91dc0aea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Thu, 29 Jul 2021 15:08:05 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E8=8F=9C=E5=8D=95=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E7=B1=BB=E7=A7=BB=E9=99=A4=E8=8F=9C=E5=8D=95=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E5=94=AF=E4=B8=80=E9=AA=8C=E8=AF=81=EF=BC=9B=20=E7=BB=86?= =?UTF-8?q?=E8=8A=82=E8=B0=83=E6=95=B4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Modules/Admin/Entities/Rabc/AdminMenu.php | 2 +- .../Admin/Http/Requests/Rabc/AdminMenuRequest.php | 10 +++++----- app/Modules/Admin/Http/Requests/Rabc/AdminRequest.php | 1 + app/Modules/Admin/Services/AdminRoleService.php | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/Modules/Admin/Entities/Rabc/AdminMenu.php b/app/Modules/Admin/Entities/Rabc/AdminMenu.php index c599b96..ebe65aa 100644 --- a/app/Modules/Admin/Entities/Rabc/AdminMenu.php +++ b/app/Modules/Admin/Entities/Rabc/AdminMenu.php @@ -16,7 +16,7 @@ class AdminMenu extends Model public function getSelectLists() { - return list_to_tree($this->orderBy('menu_sort', 'ASC')->get()->toArray()); + return list_to_tree($this->select(['menu_id', 'menu_name', 'parent_id'])->orderBy('menu_sort', 'ASC')->get()->toArray()); } public function getMenusByIds(array $menu_ids) diff --git a/app/Modules/Admin/Http/Requests/Rabc/AdminMenuRequest.php b/app/Modules/Admin/Http/Requests/Rabc/AdminMenuRequest.php index be8ffb6..ed6fe32 100644 --- a/app/Modules/Admin/Http/Requests/Rabc/AdminMenuRequest.php +++ b/app/Modules/Admin/Http/Requests/Rabc/AdminMenuRequest.php @@ -7,10 +7,10 @@ use App\Modules\Admin\Http\Requests\BaseRequest; class AdminMenuRequest extends BaseRequest { - public function setInstance() - { - $this->instance = AdminMenu::getInstance(); - } + // public function setInstance() + // { + // $this->instance = AdminMenu::getInstance(); + // } /** * Get the validation rules that apply to the request. @@ -23,7 +23,7 @@ class AdminMenuRequest extends BaseRequest 'menu_name' => [ 'required', 'max:256', - 'unique:' . $this->instance->getTable() . ',menu_name' . $this->validate_id + // 'unique:' . $this->instance->getTable() . ',menu_name' . $this->validate_id ], ]; } diff --git a/app/Modules/Admin/Http/Requests/Rabc/AdminRequest.php b/app/Modules/Admin/Http/Requests/Rabc/AdminRequest.php index 5b59750..d912f45 100644 --- a/app/Modules/Admin/Http/Requests/Rabc/AdminRequest.php +++ b/app/Modules/Admin/Http/Requests/Rabc/AdminRequest.php @@ -46,6 +46,7 @@ class AdminRequest extends BaseRequest { return [ 'admin_name.required' => '请输入管理员账户!', + 'admin_name.unique' => '管理员账户已存在!', 'password.confirmed' => '密码确认不匹配!', ]; } diff --git a/app/Modules/Admin/Services/AdminRoleService.php b/app/Modules/Admin/Services/AdminRoleService.php index d39de37..3c02857 100644 --- a/app/Modules/Admin/Services/AdminRoleService.php +++ b/app/Modules/Admin/Services/AdminRoleService.php @@ -40,7 +40,7 @@ class AdminRoleService extends BaseService if (!empty($search)){ $query->where('role_name', 'LIKE', '%' . trim($search) . '%'); } - })->with(['menus'])->orderBy($this->model->getKeyName(), 'ASC')->limit(100)->get(); + })->where('is_check', 1)->select(['role_id', 'role_name', 'role_remarks'])->orderBy($this->model->getKeyName(), 'ASC')->limit(100)->get(); } public function create(array $params) -- Gitee From 386fa52a0eaa3a183fc3e562ac0014f169355e36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Thu, 5 Aug 2021 08:28:40 +0000 Subject: [PATCH 2/8] update README.md. --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 30010fc..715e20b 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ ## 作者 [小丑路人](https://www.cnpscy.com) +## 社区 +[小丑路人社区](https://bbs.cnpscy.com) + #### 介绍 [demo](http://laravel-vue-admin.cnpscy.com/admin) -- Gitee From 2db89f324295578c4472379bfafcc3ff7042841e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Wed, 29 Sep 2021 09:40:00 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Exceptions/Handler.php | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 48a5099..e3e9edc 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,7 +3,10 @@ namespace App\Exceptions; use App\Traits\Json; +use Illuminate\Validation\ValidationException; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Throwable; class Handler extends ExceptionHandler @@ -19,8 +22,8 @@ class Handler extends ExceptionHandler \Illuminate\Auth\AuthenticationException::class, \Illuminate\Auth\Access\AuthorizationException::class, \Symfony\Component\HttpKernel\Exception\HttpException::class, - \Illuminate\Database\Eloquent\ModelNotFoundException::class, - \Illuminate\Validation\ValidationException::class, + ModelNotFoundException::class, + ValidationException::class, ]; /** @@ -57,11 +60,30 @@ class Handler extends ExceptionHandler */ public function render($request, Throwable $exception) { + // 路由404异常监听 + if($exception instanceof NotFoundHttpException){ + $this->setHttpCode(404); + return $this->errorJson("路由{{$request->path()}}不存在!"); + } + + // 模型不存在 + if ($exception instanceof ModelNotFoundException){ + return $this->errorJson($exception->getMessage()); + } + // 验证器类的错误监听 - if($exception instanceof \Illuminate\Validation\ValidationException){ + if($exception instanceof ValidationException){ return $this->errorJson($exception->validator->errors()->first()); } + // Exception类的错误监听 + if($exception instanceof \Exception){ + return $this->errorJson($exception->getMessage(), $exception->getCode(), [], [ + 'file' => $exception->getFile(), + 'line' => $exception->getLine(), + ]); + } + return parent::render($request, $exception); } } -- Gitee From e538a5d71de15f08193fc922035cd27af0df6327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Tue, 19 Oct 2021 16:29:21 +0800 Subject: [PATCH 4/8] =?UTF-8?q?Admin=E7=9A=84base=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=B1=82=EF=BC=8C=E5=AE=8C=E5=96=84=E7=BC=96=E8=BE=91=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Modules/Admin/Services/BaseService.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/Modules/Admin/Services/BaseService.php b/app/Modules/Admin/Services/BaseService.php index 891528e..a515387 100644 --- a/app/Modules/Admin/Services/BaseService.php +++ b/app/Modules/Admin/Services/BaseService.php @@ -2,6 +2,7 @@ namespace App\Modules\Admin\Services; +use App\Exceptions\Exception; use App\Models\MonthModel; use App\Services\Service; @@ -65,6 +66,9 @@ class BaseService extends Service */ public function create(array $params) { + $this->setError('新增成功!'); + // 新增时,移除唯一标识 + unset($params[$this->model->getKeyName()]); return $this->detail = $this->model->create($this->model->setFilterFields($params)); } @@ -78,7 +82,13 @@ class BaseService extends Service public function update(array $params) { $primaryKey = $this->model->getKeyName(); + if (!isset($params[$primaryKey])){ + throw new Exception('请设置主键'); + } $this->detail = $this->model->find($params[$primaryKey]); + if (!$this->detail){ + throw new Exception('编辑信息不存在!'); + } foreach ($this->model->setFilterFields($params) as $field => $value){ $this->detail->$field = $value ?? ''; } -- Gitee From b4c278bb7df146c06f759d9017a5797fac358470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Wed, 20 Oct 2021 11:45:54 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E7=9A=84=E5=81=87=E5=88=A0=E9=99=A4trait=E7=B1=BB=E4=B8=8E?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/SoftDelete/DeleteScope.php | 143 +++++++++++++++++++ app/Models/SoftDelete/SoftDelete.php | 194 ++++++++++++++++++++++++++ 2 files changed, 337 insertions(+) create mode 100644 app/Models/SoftDelete/DeleteScope.php create mode 100644 app/Models/SoftDelete/SoftDelete.php diff --git a/app/Models/SoftDelete/DeleteScope.php b/app/Models/SoftDelete/DeleteScope.php new file mode 100644 index 0000000..931b414 --- /dev/null +++ b/app/Models/SoftDelete/DeleteScope.php @@ -0,0 +1,143 @@ +getIsDelete() == 0) $builder->where($model->getQualifiedDeletedAtColumn(), $model->getIsDelete()); + } + + /** + * Extend the query builder with the needed functions. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + public function extend(Builder $builder) + { + foreach ($this->extensions as $extension) { + $this->{"add{$extension}"}($builder); + } + + // 假删除时,才需要更新字段状态 + if ($builder->getModel()->getIsDelete() == 0){ + $builder->onDelete(function (Builder $builder) { + $column = $this->getDeletedAtColumn($builder); + + return $builder->update([ + $column => 1, + ]); + }); + } + } + + /** + * Get the "deleted at" column for the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return string + */ + protected function getDeletedAtColumn(Builder $builder) + { + if (count((array) $builder->getQuery()->joins) > 0) { + return $builder->getModel()->getQualifiedDeletedAtColumn(); + } + + return $builder->getModel()->getDeletedAtColumn(); + } + + /** + * Add the restore extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addRestore(Builder $builder) + { + $builder->macro('restore', function (Builder $builder) { + $builder->withTrashed(); + + return $builder->update([$builder->getModel()->getDeletedAtColumn() => 0]); + }); + } + + /** + * Add the with-trashed extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithTrashed(Builder $builder) + { + $builder->macro('withTrashed', function (Builder $builder, $withTrashed = true) { + if (! $withTrashed) { + return $builder->withoutTrashed(); + } + + return $builder->withoutGlobalScope($this); + }); + } + + /** + * Add the without-trashed extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithoutTrashed(Builder $builder) + { + $builder->macro('withoutTrashed', function (Builder $builder) { + $model = $builder->getModel(); + + $builder->withoutGlobalScope($this)->where($model->getQualifiedDeletedAtColumn(), 0); + + return $builder; + }); + } + + /** + * Add the only-trashed extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addOnlyTrashed(Builder $builder) + { + $builder->macro('onlyTrashed', function (Builder $builder) { + $model = $builder->getModel(); + + $builder->withoutGlobalScope($this)->where($model->getQualifiedDeletedAtColumn(), 0); + + return $builder; + }); + } +} diff --git a/app/Models/SoftDelete/SoftDelete.php b/app/Models/SoftDelete/SoftDelete.php new file mode 100644 index 0000000..09807b7 --- /dev/null +++ b/app/Models/SoftDelete/SoftDelete.php @@ -0,0 +1,194 @@ +is_delete; + } + + public function getDeleteField() + { + return static::DELETE_FIELD; + } + + /** + * Boot the soft deleting trait for a model. + * + * @return void + */ + public static function bootSoftDelete() + { + static::addGlobalScope(new DeleteScope()); + } + + /** + * Force a hard delete on a soft deleted model. + * + * @return bool|null + */ + public function forceDelete() + { + $this->is_delete = 1; + + return tap($this->delete(), function ($deleted) { + $this->is_delete = 0; + + if ($deleted) { + $this->fireModelEvent('forceDeleted', false); + } + }); + } + + /** + * Perform the actual delete query on this model instance. + * + * @return mixed + */ + protected function performDeleteOnModel() + { + if ($this->is_delete) { + $this->exists = false; + + return $this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(); + } + + return $this->runSoftDelete(); + } + + /** + * Perform the actual delete query on this model instance. + * + * @return void + */ + protected function runSoftDelete() + { + $query = $this->setKeysForSaveQuery($this->newModelQuery()); + + $time = $this->freshTimestamp(); + + $columns = [$this->getDeletedAtColumn() => 1]; + + $this->{$this->getDeletedAtColumn()} = $time; + + if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) { + $this->{$this->getUpdatedAtColumn()} = $time; + + $columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time); + } + + $query->update($columns); + + $this->syncOriginalAttributes(array_keys($columns)); + + $this->fireModelEvent('trashed', false); + } + + /** + * Restore a soft-deleted model instance. + * + * @return bool|null + */ + public function restore() + { + // If the restoring event does not return false, we will proceed with this + // restore operation. Otherwise, we bail out so the developer will stop + // the restore totally. We will clear the deleted timestamp and save. + if ($this->fireModelEvent('restoring') === false) { + return false; + } + + $this->{$this->getDeletedAtColumn()} = null; + + // Once we have saved the model, we will fire the "restored" event so this + // developer will do anything they need to after a restore operation is + // totally finished. Then we will return the result of the save call. + $this->exists = true; + + $result = $this->save(); + + $this->fireModelEvent('restored', false); + + return $result; + } + + /** + * Determine if the model instance has been soft-deleted. + * + * @return bool + */ + public function trashed() + { + return ! is_null($this->{$this->getDeletedAtColumn()}); + } + + /** + * Register a "softDeleted" model event callback with the dispatcher. + * + * @param \Closure|string $callback + * @return void + */ + public static function softDeleted($callback) + { + static::registerModelEvent('trashed', $callback); + } + + /** + * Register a "restoring" model event callback with the dispatcher. + * + * @param \Closure|string $callback + * @return void + */ + public static function restoring($callback) + { + static::registerModelEvent('restoring', $callback); + } + + /** + * Register a "restored" model event callback with the dispatcher. + * + * @param \Closure|string $callback + * @return void + */ + public static function restored($callback) + { + static::registerModelEvent('restored', $callback); + } + + /** + * Register a "forceDeleted" model event callback with the dispatcher. + * + * @param \Closure|string $callback + * @return void + */ + public static function forceDeleted($callback) + { + static::registerModelEvent('forceDeleted', $callback); + } + + /** + * Get the name of the "deleted at" column. + * + * @return string + */ + public function getDeletedAtColumn() + { + return defined('static::DELETED_AT') ? static::DELETED_FIELD : 'is_delete'; + } + + /** + * Get the fully qualified "deleted at" column. + * + * @return string + */ + public function getQualifiedDeletedAtColumn() + { + return $this->qualifyColumn($this->getDeletedAtColumn()); + } +} -- Gitee From dc914b7bfb8b79a2e406f0d7db8cfc4493f26468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Wed, 20 Oct 2021 11:50:14 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=BB=A7=E6=89=BF?= =?UTF-8?q?=E5=81=87=E5=88=A0=E9=99=A4trait=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/Model.php | 32 ++------------------------------ app/Scopes/DeleteScope.php | 31 ------------------------------- 2 files changed, 2 insertions(+), 61 deletions(-) delete mode 100644 app/Scopes/DeleteScope.php diff --git a/app/Models/Model.php b/app/Models/Model.php index ccdea0b..ee229d8 100644 --- a/app/Models/Model.php +++ b/app/Models/Model.php @@ -2,7 +2,7 @@ namespace App\Models; -use App\Scopes\DeleteScope; +use App\Models\SoftDelete\SoftDelete; use App\Traits\Instance; use App\Traits\MysqlTable; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\Model as EloquentModel; class Model extends EloquentModel { + use SoftDelete; use MysqlTable; use Instance; use HasFactory; @@ -48,22 +49,6 @@ class Model extends EloquentModel return $this->attributes[self::UPDATED_AT]; } - /** - * 自定义的软删除 - */ - protected $is_delete = 1; //是否开启删除(1.开启删除,就是直接删除;0.假删除) - protected $delete_field = 'is_delete'; //删除字段 - - public function getIsDelete() - { - return $this->is_delete; - } - - public function getDeleteField() - { - return $this->delete_field; - } - /** * 不可批量赋值的属性 * @@ -71,19 +56,6 @@ class Model extends EloquentModel */ protected $guarded = []; - /** - * 模型的 "booted" 方法 - * - * 应用全局作用域 - * - * @return void - */ - protected static function booted() - { - // 假删除的作用域 - static::addGlobalScope(new DeleteScope(new static)); - } - public static function firstByWhere($where) { return self::where($where)->first(); diff --git a/app/Scopes/DeleteScope.php b/app/Scopes/DeleteScope.php deleted file mode 100644 index 6fe95f3..0000000 --- a/app/Scopes/DeleteScope.php +++ /dev/null @@ -1,31 +0,0 @@ -is_delete = $model->getIsDelete(); - $this->delete_field = $model->getDeleteField(); - } - - /** - * 把约束加到 Eloquent 查询构造中 - * - * @param \Illuminate\Database\Eloquent\Builder $builder - * @param \Illuminate\Database\Eloquent\Model $model - * @return void - */ - public function apply(Builder $builder, Model $model) - { - if ($this->is_delete == 0) $builder->where($this->delete_field, $this->is_delete); - } -} -- Gitee From bf0e067999766983dfbffe9e7be4b8215f9ab6b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Wed, 20 Oct 2021 12:01:35 +0800 Subject: [PATCH 7/8] update uploadfile. --- app/Modules/Admin/Http/Controllers/UploadController.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/Modules/Admin/Http/Controllers/UploadController.php b/app/Modules/Admin/Http/Controllers/UploadController.php index ac287ff..a04bf27 100644 --- a/app/Modules/Admin/Http/Controllers/UploadController.php +++ b/app/Modules/Admin/Http/Controllers/UploadController.php @@ -11,6 +11,7 @@ class UploadController extends BaseController * 文件上传 * * @param \Illuminate\Http\Request $request + * @param string $dir_path 文件夹 * @param string $file * * @return \Illuminate\Http\JsonResponse @@ -21,14 +22,10 @@ class UploadController extends BaseController return $this->errorJson('请上传文件!'); } - // var_dump($request->file($file)->getClientOriginalExtension()); - // var_dump($request->file($file)->getMimeType()); - // var_dump($request->file($file)->getType()); - // var_dump($request->file($file)->getClientMimeType()); - // exit; + $dir_path = (empty($request->dir_path) ? '' : ($request->dir_path . '/')) . date('Ym'); $path = $request->file($file)->storePublicly( - date('Ym'), + $dir_path, config('filesystems') ); -- Gitee From 28b0ef6544632d5fe20f503de286adaa0508174d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Fri, 22 Oct 2021 15:49:36 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E7=9A=84=E5=81=87=E5=88=A0=E9=99=A4=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?baseservice=E6=97=A0=E9=9C=80=E5=88=A4=E6=96=AD=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E6=98=AF=E5=90=A6=E5=81=87=E5=88=A0=E9=99=A4=EF=BC=8C?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=B0=83=E7=94=A8delete=E5=8D=B3=E5=8F=AF?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=81=87=E5=88=A0=E9=99=A4=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/SoftDelete/DeleteScope.php | 10 +++++----- app/Modules/Admin/Services/BaseService.php | 6 +----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/app/Models/SoftDelete/DeleteScope.php b/app/Models/SoftDelete/DeleteScope.php index 931b414..ff1a32b 100644 --- a/app/Models/SoftDelete/DeleteScope.php +++ b/app/Models/SoftDelete/DeleteScope.php @@ -44,12 +44,12 @@ class DeleteScope implements Scope */ public function extend(Builder $builder) { - foreach ($this->extensions as $extension) { - $this->{"add{$extension}"}($builder); - } - - // 假删除时,才需要更新字段状态 + // 假删除时,才需要调用扩展方法与更新字段状态 if ($builder->getModel()->getIsDelete() == 0){ + foreach ($this->extensions as $extension) { + $this->{"add{$extension}"}($builder); + } + $builder->onDelete(function (Builder $builder) { $column = $this->getDeletedAtColumn($builder); diff --git a/app/Modules/Admin/Services/BaseService.php b/app/Modules/Admin/Services/BaseService.php index a515387..9189b67 100644 --- a/app/Modules/Admin/Services/BaseService.php +++ b/app/Modules/Admin/Services/BaseService.php @@ -120,11 +120,7 @@ class BaseService extends Service if ($this->model instanceof MonthModel && isset($params['month'])){ $this->model = $this->model->setMonthTable($params['month']); } - if ($this->model->getIsDelete() == 0){ - return $this->model->whereIn($primaryKey, $ids)->update([$this->model->getDeleteField() => 1]); - }else{ - return $this->model->whereIn($primaryKey, $ids)->delete(); - } + return $this->model->whereIn($primaryKey, $ids)->delete(); } /** -- Gitee