validate([ 'province' => ['nullable', 'string', 'max:32'], 'province_id' => ['nullable', 'integer'], 'region' => ['nullable', 'string', 'max:32'], 'type' => ['nullable', 'in:city,municipality,sub_municipality'], ]); $cities = CityMunicipality::query() ->when($request->filled('province'), function ($q) use ($request) { $q->whereHas('province', fn ($p) => $p->where('psgc_code', $request->string('province')) ->orWhere('code', $request->string('province'))); }) ->when($request->filled('province_id'), fn ($q) => $q->where('province_id', $request->integer('province_id'))) ->when($request->filled('region'), function ($q) use ($request) { $q->whereHas('province.region', fn ($r) => $r->where('psgc_code', $request->string('region')) ->orWhere('code', $request->string('region'))); }) ->when($request->filled('type'), fn ($q) => $q->where('type', $request->string('type'))) ->orderBy('name') ->get(); return $this->ok(CityMunicipalityResource::collection($cities)); } }