boolean('active_only', false); $search = trim((string) $request->query('search', '')); $countries = Country::query() ->when($activeOnly, fn ($query) => $query->where('is_active', true)) ->when($search !== '', function ($query) use ($search): void { $query->where(function ($nested) use ($search): void { $nested->where('name', 'ilike', "%{$search}%") ->orWhere('official_name', 'ilike', "%{$search}%") ->orWhere('common_name', 'ilike', "%{$search}%") ->orWhere('display_name', 'ilike', "%{$search}%") ->orWhere('native_name', 'ilike', "%{$search}%") ->orWhere('alpha2', 'ilike', "%{$search}%") ->orWhere('alpha3', 'ilike', "%{$search}%") ->orWhere('numeric', 'ilike', "%{$search}%") ->orWhere('region', 'ilike', "%{$search}%") ->orWhere('subregion', 'ilike', "%{$search}%") ->orWhere('currency_code', 'ilike', "%{$search}%") ->orWhere('calling_code', 'ilike', "%{$search}%"); }); }) ->ordered() ->get(); return response()->json(['data' => $countries]); } /** * List active ISO 3166-1 countries for business selectors. */ public function active(): JsonResponse { $countries = Country::query()->active()->ordered()->get(); return response()->json(['data' => $countries]); } /** * Show one country by alpha-2 route binding. */ public function show(Country $country): JsonResponse { return response()->json(['data' => $country->loadCount('subdivisions')]); } /** * Resolve alpha-2, alpha-3 or numeric ISO 3166-1 country code. */ public function resolve(string $code, CountryRepository $countries): JsonResponse { $country = $countries->findByIsoCode($code); abort_if($country === null, 404, 'Country code not found.'); return response()->json(['data' => $country]); } /** * List ISO 3166-2 subdivisions for the supplied ISO 3166-1 alpha-2 country. */ public function subdivisions(string $alpha2, Request $request, CountryRepository $countries): JsonResponse { $activeOnly = $request->boolean('active_only', true); $country = $countries->findByAlpha2($alpha2); abort_if($country === null, 404, 'Country not found.'); $subdivisions = CountrySubdivision::query() ->where('country_id', $country->id) ->when($activeOnly, fn ($query) => $query->where('is_active', true)) ->ordered() ->get(); return response()->json(['data' => $subdivisions]); } }