validate([ 'city' => ['nullable', 'string', 'max:32'], 'city_id' => ['nullable', 'integer'], 'q' => ['nullable', 'string', 'max:100'], ]); $barangays = Barangay::query() ->when($request->filled('city'), function ($q) use ($request) { $q->whereHas('cityMunicipality', fn ($c) => $c->where('psgc_code', $request->string('city')) ->orWhere('code', $request->string('city'))); }) ->when($request->filled('city_id'), fn ($q) => $q->where('city_municipality_id', $request->integer('city_id'))) ->when($request->filled('q'), fn ($q) => $q->where('name', 'like', '%'.$request->string('q').'%')) ->orderBy('name') ->limit(500) ->get(); return $this->ok(BarangayResource::collection($barangays)); } }