tenantId()) ->orderByDesc('id'); if ($request->filled('company')) { $query->where('company_name', 'like', '%' . $request->company . '%'); } if ($request->filled('material_name')) { $query->where('material_name', 'like', '%' . $request->material_name . '%'); } if ($request->filled('search')) { $s = $request->search; $query->where(function ($q) use ($s) { $q->where('material_name', 'like', "%{$s}%") ->orWhere('material_code', 'like', "%{$s}%") ->orWhere('specification', 'like', "%{$s}%"); }); } $perPage = $request->integer('per_page', 15); $materials = $query->paginate($perPage); return response()->json($materials); } public function store(Request $request): JsonResponse { $validated = $request->validate([ 'company_name' => 'required|string|max:200', 'material_code' => 'nullable|string|max:50', 'material_name' => 'required|string|max:200', 'specification' => 'nullable|string|max:300', 'unit' => 'nullable|string|max:50', 'design_quantity' => 'nullable|numeric|min:0', ]); $validated['tenant_id'] = $this->tenantId(); $validated['design_quantity'] = $validated['design_quantity'] ?? 0; $material = PmisMaterial::create($validated); return response()->json($material, 201); } public function update(Request $request, int $id): JsonResponse { $material = PmisMaterial::tenant($this->tenantId())->findOrFail($id); $validated = $request->validate([ 'company_name' => 'sometimes|required|string|max:200', 'material_code' => 'nullable|string|max:50', 'material_name' => 'sometimes|required|string|max:200', 'specification' => 'nullable|string|max:300', 'unit' => 'nullable|string|max:50', 'design_quantity' => 'nullable|numeric|min:0', ]); $material->update($validated); return response()->json($material); } public function destroy(int $id): JsonResponse { $material = PmisMaterial::tenant($this->tenantId())->findOrFail($id); $material->delete(); return response()->json(['message' => '삭제되었습니다.']); } }