actionOfQuickEdit()) { $result = $this->service->quickEdit($request->all()); } elseif ($this->actionOfQuickEditItem()) { $result = $this->service->quickEditItem($request->all()); } else { $result = $this->service->store($request->all()); } if (! $result) { admin_abort($this->service->getError() ?: __('admin.save').__('admin.failed')); } DB::commit(); } catch (Throwable $th) { DB::rollBack(); return $this->renderException($th); } return $this->response()->successMessage(__('admin.save').__('admin.successfully')); } /** * {@inheritdoc} */ public function update(Request $request) { $input = Arr::except($request->all(), $this->service->primaryKey()); if ($request->filled('_fields')) { $input = Arr::only($input, explode(',', $request->input('_fields'))); } try { DB::beginTransaction(); $result = $this->service->update($this->getPrimaryValue($request), $input); if (! $result) { admin_abort($this->service->getError() ?: __('admin.save').__('admin.failed')); } DB::commit(); } catch (Throwable $th) { DB::rollBack(); return $this->renderException($th); } return $this->response()->successMessage(__('admin.save').__('admin.successfully')); } /** * {@inheritdoc} */ public function destroy($ids) { try { DB::beginTransaction(); $result = $this->service->delete($ids); if ($result === false) { admin_abort($this->service->getError()); } DB::commit(); } catch (Throwable $th) { DB::rollBack(); return $this->renderException($th); } return $this->response()->successMessage(__('admin.delete').__('admin.successfully')); } public function getQuickEditItemPath(array $fields = ['*']) { $path = $this->getUpdatePath(); if ($fields != ['*']) { $path .= '?_fields='.implode(',', $fields); } return $path; } protected function renderException(Throwable $e) { report($e); if ($e instanceof AdminException) { return $e->render(); } $message = $e->getMessage(); if ($e instanceof ValidationException) { $message = Arr::first($e->errors()); if (is_array($message)) { $message = Arr::first($message) ?: '参数错误'; } } return $this->response()->fail($message); } }