gate('项目耗材'); $ownerMaterials = OwnerMaterial::query()->with(['owner','material','initiator','file'])->orderByDesc('id')->filter($filters)->paginate($request['paginate'] ?? 50); $owners = app(OwnerService::class)->getAuthorizedOwners(); $materials = Material::all(); return view('/maintenance/ownerMaterial/index',compact('ownerMaterials','owners','materials')); } public function storeApi(Request $request):array { $this->gate('项目耗材-编辑'); $params = $request->all(['owner_id', 'material_id', 'material_code', 'size', 'special', 'specification']); $params['initiator'] = Auth::user()['id']; try { $material = OwnerMaterial::query()->create($params); if ($material) return ['success' => true,'data' => $material->loadMissing(['owner','material','initiator','file'])]; else return ['success' => false, 'message' => '添加失败']; } catch (\Exception $e) { return ['success' => false,'message' => $e->getMessage()]; } } public function updateApi(Request $request) { $this->gate('项目耗材-编辑'); $material = OwnerMaterial::query()->find($request['id']); if(!$material)$this->error("对应的项目耗材没有找到"); $bool = $material->update($request->all(['owner_id', 'material_id', 'material_code', 'size', 'special', 'specification'])); if($bool==0)$this->error("更新异常"); $this->success($material->loadMissing(['owner','material','initiator','file'])); } public function destroyApi($id) { $this->gate('项目耗材-删除'); $ownerMaterial = OwnerMaterial::query()->find($id); if(!$ownerMaterial)$this->error('对应的项目耗材没有找到'); $ownerMaterial->delete(); $this->success(); } public function fileApi(Request $request) { $this->gate('项目耗材-文件上传'); $ownerMaterial = OwnerMaterial::query()->find($request['id']); if(!$ownerMaterial)$this->error('项目耗材不存在'); try { $file = $request->file('file'); $fileType = $file->getMimeType();// 文件类型 $fileName = strtolower($file->getClientOriginalExtension());// 文件名 $path = $file->storeAs("ownerMaterial", Uuid::uuid() . "." . $fileName); $file = file::query()->create([ 'table_name' => 'owner_material', 'table_id' => $ownerMaterial['id'], 'url' => $path, 'type' => $fileType, "file_name" => $file->getClientOriginalName() ]); $this->success($ownerMaterial->loadMissing(['owner', 'material', 'initiator', 'file'])); } catch (Exception $e) { $this->error($e->getMessage()); } } }