validate([ 'status' => ['nullable', 'in:active,inactive'], 'q' => ['nullable', 'string', 'max:100'], ]); $areas = ServiceArea::query() ->withCount('barangays') ->when($request->filled('status'), fn ($q) => $q->where('status', $request->string('status'))) ->when($request->filled('q'), fn ($q) => $q->where('name', 'like', '%'.$request->string('q').'%')) ->orderBy('name') ->get(); return $this->ok(ServiceAreaResource::collection($areas)); } public function store(StoreServiceAreaRequest $request): JsonResponse { $data = $request->validated(); $barangayIds = $data['barangay_ids'] ?? []; unset($data['barangay_ids']); $area = DB::transaction(function () use ($data, $barangayIds) { $area = ServiceArea::create($data); if (! empty($barangayIds)) { $area->barangays()->sync($barangayIds); } return $area; }); return $this->created( new ServiceAreaResource($area->loadCount('barangays')), 'Service area created', ); } public function show(ServiceArea $serviceArea): JsonResponse { $serviceArea->load(['barangays.cityMunicipality.province']) ->loadCount('barangays'); return $this->ok(new ServiceAreaResource($serviceArea)); } public function update(UpdateServiceAreaRequest $request, ServiceArea $serviceArea): JsonResponse { $data = $request->validated(); $barangayIds = $data['barangay_ids'] ?? null; unset($data['barangay_ids']); DB::transaction(function () use ($serviceArea, $data, $barangayIds) { $serviceArea->update($data); if ($barangayIds !== null) { $serviceArea->barangays()->sync($barangayIds); } }); return $this->ok( new ServiceAreaResource($serviceArea->fresh()->loadCount('barangays')), 'Service area updated', ); } public function destroy(ServiceArea $serviceArea): JsonResponse { $serviceArea->delete(); return $this->ok(null, 'Service area deleted'); } public function attachBarangays(AttachBarangaysRequest $request, ServiceArea $serviceArea): JsonResponse { $serviceArea->barangays()->syncWithoutDetaching($request->validated('barangay_ids')); return $this->ok( new ServiceAreaResource($serviceArea->load('barangays')->loadCount('barangays')), 'Barangays attached', ); } public function detachBarangay(ServiceArea $serviceArea, Barangay $barangay): JsonResponse { $serviceArea->barangays()->detach($barangay->id); return $this->ok(null, 'Barangay detached'); } }