PHP code example of danielebuso / laravel-countries
1. Go to this page and download the library: Download danielebuso/laravel-countries library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
danielebuso / laravel-countries example snippets
return [
/*
* The default locale to use when retrieving country names.
* If not set, it will use Laravel's app locale.
*/
'locale' => null,
/*
* Cache configuration for country data.
* Sushi automatically caches the data, but you can configure it here.
*/
'cache' => [
'enabled' => true,
'ttl' => 3600, // Time in seconds
],
];
use Danielebuso\LaravelCountries\Models\Country;
// Get all countries
$countries = Country::all();
// Find a country by alpha-2 code
$usa = Country::where('alpha2', 'US')->first();
// Find a country by alpha-3 code
$italy = Country::where('alpha3', 'ITA')->first();
// Get countries sorted by name
$countries = Country::orderBy('name')->get();
use Danielebuso\LaravelCountries\Models\Country;
// Get country name in a specific language
$italy = Country::where('alpha2', 'IT')->first();
// Get name in Italian
$italianName = $italy->getName('it'); // Returns "Italia"
// Get name in Spanish
$spanishName = $italy->getName('es'); // Returns "Italia"
// Get name in French
$frenchName = $italy->getName('fr'); // Returns "Italie"
// Get name in Croatian
$croatianName = $italy->getName('hr'); // Returns "Italija"
// Get name using app's current locale
app()->setLocale('es');
$localizedName = $italy->getName(); // Returns "Italia" (Spanish)
// Search countries by name
$countries = Country::where('name', 'LIKE', '%United%')->get();
// Get a specific country
$germany = Country::where('alpha2', 'DE')->first();
echo $germany->name; // Germany
echo $germany->alpha3; // DEU
echo $germany->id; // 276
// Count all countries
$total = Country::count();
// Pagination
$countries = Country::paginate(20);