query()->orderByDesc('sort')->get()->toArray(); return array2tree($list); } public function getModelFilter() { return $this->modelFilterName; } public function listQuery() { $model = $this->getModel(); $filter = $this->getModelFilter(); $query = $this->query(); if ($this->withRelationships) { $query->with($this->withRelationships); } if ($filter) { $query->filter(request()->input(), $filter); } return $query->orderByDesc($model->getUpdatedAtColumn() ?? $model->getKeyName()); } public function list() { $query = $this->listQuery(); $list = (clone $query)->paginate(request()->input('perPage', 20)); $items = $list->items(); $total = $list->total(); return compact('items', 'total'); } public function getDetail($id) { return $this->query()->with($this->withRelationships)->find($id); } public function store($data): bool { $data = $this->resloveData($data); $validate = $this->validate($data); if ($validate !== true) { $this->setError($validate); return false; } $this->modelName::create($data); return true; } public function update($primaryKey, $data): bool { $data = $this->resloveData($data); $model = $this->query()->whereKey($primaryKey)->first(); $validate = $this->validate($data, $model->id); if ($validate !== true) { $this->setError($validate); return false; } return $model->update($data); } /** * 处理表单数据 * * @param array $data * @return array */ public function resloveData($data) { return $data; } /** * 表单验证 * * @param array $data * @param int $id 空: 添加, 非空: 修改 * @return mixed true: 验证通过, string: 错误提示 */ public function validate($data, $id = null) { return true; } }