all(), [ 'code' => 'required|string|unique:country,code', 'name' => 'required', ]); if ($validator->fails()) { return response()->json(['status' => 'fail', 'message' => $validator->errors()->all()]); } try { if (Country::create($validator->validated())) { return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.add')])]); } } catch (Exception $e) { Log::error(trans('common.error_action_item', ['action' => trans('common.add'), 'attribute' => trans('model.node.country')]).': '.$e->getMessage()); return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')]).', '.$e->getMessage()]); } return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')])]); } public function update(Request $request, Country $country): JsonResponse { // 编辑国家/地区 $validator = Validator::make($request->all(), ['name' => 'required']); if ($validator->fails()) { return response()->json(['status' => 'fail', 'message' => $validator->errors()->all()]); } try { if ($country->update($validator->validated())) { return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.edit')])]); } } catch (Exception $e) { Log::error(trans('common.error_action_item', ['action' => trans('common.edit'), 'attribute' => trans('model.node.country')]).': '.$e->getMessage()); return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')]).', '.$e->getMessage()]); } return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')])]); } public function destroy(Country $country): JsonResponse { // 删除国家/地区 // 校验该国家/地区下是否存在关联节点 if ($country->nodes()->exists()) { return response()->json(['status' => 'fail', 'message' => trans('common.exists_error', ['attribute' => trans('model.node.country')])]); } try { if ($country->delete()) { return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.delete')])]); } } catch (Exception $e) { Log::error(trans('common.error_action_item', ['action' => trans('common.delete'), 'attribute' => trans('model.node.country')]).': '.$e->getMessage()); return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')]).', '.$e->getMessage()]); } return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')])]); } }