model = new Template(); } /** * 模板列表 */ public function index(Request $request): Response { if ($request->isAjax()) { $param = $request->get(); if ($param['subResource'] ?? false) { $keyword = trim($param['keyword'] ?? ''); $subResource = []; switch ($param['subResource']) { default: return sparkSuccess(dataReturn(-1, '未知子资源')); } return sparkSuccess(dataReturn(0, '获取子资源成功', $subResource)); } $param['pagination'] ??= ['page' => 1, 'limit' => 10]; $query = $this->model::with([]); return sparkSuccess(dataReturn(0, '获取成功', $query->paginate($param['pagination']['limit'], ['*'], 'page', $param['pagination']['page']))); } return view('template/index'); } /** * 查模板 */ public function show(Request $request, $id): Response { $show = $this->model::with([])->find($id); return view('template/index', ['show' => $show]); } /** * 增模板 */ public function store(Request $request): Response { $data = $request->post(); // 过滤黑名单 $blacklist = ['']; $data = Arr::except($data, $blacklist); $this->model::insert($data); // $that = $this; // $that->model->executeTransaction(function () use ($that, $data) { // $model = $that->model::create($data); // }); return sparkSuccess(dataReturn(0, '添加成功')); } /** * 改模板 */ public function update(Request $request, int $id): Response { $data = $request->post(); // 过滤黑名单 $blacklist = ['']; $data = Arr::except($data, $blacklist); $this->model::find($id)->update($data); $that = $this; // $that->model->executeTransaction(function () use ($that, $id, $data) { // $model = $that->model::find($id); // $model->update($data); // }); return sparkSuccess(dataReturn(0, '操作成功')); } /** * 删模板 */ public function destroy(Request $request, $id): Response { $id = explode(',', $id); $this->model::destroy($id); // $that = $this; // $that->model->executeTransaction(function () use ($that, $id) { // $model::destroy($id); // }); return sparkSuccess(dataReturn(0, '操作成功')); } }