1
0
Fork 0

更新 app/Admin/Services/BaseService.php

panliang 2023-09-25 10:26:40 +08:00
parent 10236732ef
commit 57506111ba
1 changed files with 24 additions and 2 deletions

View File

@ -42,7 +42,7 @@ class BaseService extends AdminService
$query->filter(request()->input(), $filter); $query->filter(request()->input(), $filter);
} }
return $query->orderByDesc($model->getUpdatedAtColumn() ?? $model->getKeyName()); return $query->orderByDesc($model->getKeyName());
} }
public function list() public function list()
@ -79,7 +79,7 @@ class BaseService extends AdminService
public function update($primaryKey, $data): bool public function update($primaryKey, $data): bool
{ {
$data = $this->resloveData($data); $data = $this->resloveData($data);
$model = $this->query()->whereKey($primaryKey)->first(); $model = $this->query()->whereKey($primaryKey)->firstOrFail();
$validate = $this->validate($data, $model->id); $validate = $this->validate($data, $model->id);
if ($validate !== true) { if ($validate !== true) {
$this->setError($validate); $this->setError($validate);
@ -89,6 +89,17 @@ class BaseService extends AdminService
return $model->update($data); 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; return true;
} }
/**
* 删除的前置方法
*
* @param array $ids 主键id
* @return mixed true: 继续后续操作, string: 中断操作, 返回错误提示
*/
public function preDelete(array $ids)
{
return true;
}
} }