validate([ 'verification_status' => ['nullable', 'in:pending,approved,rejected'], 'barangay_id' => ['nullable', 'integer'], 'q' => ['nullable', 'string', 'max:100'], 'per_page' => ['nullable', 'integer', 'min:1', 'max:100'], ]); $perPage = (int) $request->input('per_page', 25); $households = Household::query() ->with(['head', 'barangay']) ->withCount('members') ->when( $request->filled('verification_status'), fn ($q) => $q->where('verification_status', $request->string('verification_status')), ) ->when( $request->filled('barangay_id'), fn ($q) => $q->where('barangay_id', $request->integer('barangay_id')), ) ->when($request->filled('q'), function ($q) use ($request) { $term = '%'.$request->string('q').'%'; $q->where(function ($qq) use ($term) { $qq->where('address_line', 'like', $term) ->orWhereHas('head', function ($h) use ($term) { $h->where('first_name', 'like', $term) ->orWhere('last_name', 'like', $term) ->orWhere('email', 'like', $term); }); }); }) ->orderByDesc('id') ->paginate($perPage); return $this->ok( HouseholdResource::collection($households), null, [ 'page' => $households->currentPage(), 'per_page' => $households->perPage(), 'total' => $households->total(), 'last_page' => $households->lastPage(), ], ); } public function show(Household $household): JsonResponse { $household->load(['head', 'barangay.cityMunicipality.province', 'members.user']) ->loadCount('members'); return $this->ok(new HouseholdResource($household)); } public function approve(Request $request, Household $household): JsonResponse { if ($household->verification_status === Household::VERIFICATION_APPROVED) { return $this->fail('Household already approved', null, 422); } if (! $household->proof_of_residency_path) { return $this->fail( 'Cannot approve without proof of residency on file', ['proof' => ['missing']], 422, ); } $household->markVerified($request->user()); HouseholdVerified::dispatch($household->fresh(), $request->user()); $household->load(['head', 'barangay'])->loadCount('members'); return $this->ok(new HouseholdResource($household), 'Household approved'); } public function reject(RejectProfileRequest $request, Household $household): JsonResponse { $reason = $request->validated('reason'); $household->markRejected($request->user(), $reason); if ($household->head) { \Illuminate\Support\Facades\Notification::send( $household->head, new \App\Notifications\HouseholdRejected($household, $reason), ); } $household->load(['head', 'barangay'])->loadCount('members'); return $this->ok(new HouseholdResource($household), 'Household rejected'); } }