diff --git a/app/Admin/Services/BaseService.php b/app/Admin/Services/BaseService.php index a0adc38..06d3dee 100644 --- a/app/Admin/Services/BaseService.php +++ b/app/Admin/Services/BaseService.php @@ -42,7 +42,7 @@ class BaseService extends AdminService $query->filter(request()->input(), $filter); } - return $query->orderByDesc($model->getUpdatedAtColumn() ?? $model->getKeyName()); + return $query->orderByDesc($model->getKeyName()); } public function list() @@ -79,7 +79,7 @@ class BaseService extends AdminService public function update($primaryKey, $data): bool { $data = $this->resloveData($data); - $model = $this->query()->whereKey($primaryKey)->first(); + $model = $this->query()->whereKey($primaryKey)->firstOrFail(); $validate = $this->validate($data, $model->id); if ($validate !== true) { $this->setError($validate); @@ -89,6 +89,17 @@ class BaseService extends AdminService return $model->update($data); } + public function delete(string $ids): mixed + { + $id = explode(',', $ids); + $result = $this->preDelete($id); + if ($result !== true) { + $this->setError($validate); + return false; + } + return $this->query()->whereIn($this->primaryKey(), $id)->delete(); + } + /** * 处理表单数据 * @@ -111,4 +122,15 @@ class BaseService extends AdminService { return true; } + + /** + * 删除的前置方法 + * + * @param array $ids 主键id + * @return mixed true: 继续后续操作, string: 中断操作, 返回错误提示 + */ + public function preDelete(array $ids) + { + return true; + } }