Laravel 11 - Rinvex Country Details
Laravel 11 - Rinvex Country Details
In this tutorial, we'll explore how to integrate the Rinvex Countries package in Laravel 11, enabling easy access to comprehensive country data for global applications.
If you're a video person, feel free to skip the post and check out the video instead!
Quick Overview
This tutorial walks you through integrating the rinvex/countries package in a Laravel application to easily retrieve and display country-related information. We begin by setting up a fresh Laravel project configured with Pest and MySQL. After installing the Rinvex Countries package via Composer, we verify its installation by displaying a list of countries using a basic route. From there, we dive deeper into accessing specific country details such as names, flags, native names, currencies, languages, and more—using predefined helper methods. By the end of this guide, you’ll have a fully functional setup to incorporate dynamic, location-based data into your Laravel projects without maintaining your own country dataset.
Step # 1 : Initialize the Laravel Project.
Start by creating a new Laravel project or using an existing one. To create a fresh project, run.
laravel new countries
Alternatively, use Composer by running.
composer create-project laravel/laravel --prefer-dist countries
Follow these steps during the setup process.
- Choose None when prompted for a Starter Kit.
- Select Pest as the testing framework.
- Use MySQL for the database.
- When asked to run the default migrations, type yes.
This will initialize a new Laravel project named countries, configured with Pest as the testing framework and MySQL as the database. By opting for Pest, you're setting up an efficient and expressive testing environment that will help streamline your testing process as your application grows. The use of MySQL ensures compatibility with most production environments, providing a solid foundation for storing and managing your data. With these configurations in place, you'll have all the necessary tools to begin building the application, allowing you to focus on adding features and functionality without worrying about foundational setup.
Step # 2 : Navigate to Your Laravel Project.
After creating your Laravel project, open your terminal (such as Git Bash or CMD) and move into the project directory. This allows you to run Artisan commands and manage your app from the project’s root. For example, in Git Bash, run
cd c:xampp/htdocs/countries
Make sure to adjust the path according to where your Laravel project is located.
Step # 3 : Install the Rinvex Countries Package.
To work with a ready-made list of countries and their data, install the rinvex/countries package using Composer. This package provides comprehensive country-related information such as names, codes, timezones, and more making it a great fit for apps that need country selection features. In your terminal, run the following command.
composer require rinvex/countries
Composer will automatically download the package along with its dependencies and integrate it into your Laravel project. You don’t need to register any service providers manually. Laravel’s auto-discovery handles that for you. Once installed, the package is immediately available throughout your application, enabling you to start retrieving country data like names, ISO codes, currencies, and timezones with simple helper functions. This significantly speeds up development and reduces the need for maintaining your own country dataset.
Step # 4 : Define a Route to Retrieve All Countries.
Next, let’s create a simple route to fetch and display the list of all available countries provided by the Rinvex package. We’ll use the built-in countries() helper function to retrieve the data. Open your web.php file and update it with the following code.
Route::get('/', function () {
$countries = countries();
dd($countries);
});
This route will hit the root URL (/) of your Laravel application, retrieve the full collection of country data, and immediately dump it using Laravel’s dd() helper. It’s a quick way to confirm that the package is working and returning the expected information.
Step # 5 : Run the Project and Fetch Country Data.
Start by launching Laravel’s built-in development server with the following command.
php artisan serve
Once the server is running, open your browser and navigate to: http://127.0.0.1:8000.
Now, let’s test if the package is working correctly by retrieving the details of a specific country using its ISO 3166-1 alpha-2 code. For example, we’ll use 'sa', which stands for Saudi Arabia. This will help confirm that the package is properly installed and that we can successfully access country-specific data. To do this, open your web.php file and update the route as shown below.
Route::get('/', function () {
$country = country('sa');
dd($country);
});
When you visit your local URL (e.g., 127.0.0.1:8000), this route will return a detailed country object containing various data points like the country’s name, ISO codes, calling codes, currencies, languages, and more. This is a great way to verify that everything is set up properly before proceeding to more advanced usage.
To dive deeper into a specific country's data, the rinvex/countries package offers a variety of predefined methods that make it easy to retrieve comprehensive information. These methods allow you to access everything from the country's name and flag to more advanced attributes like languages, currencies, and geographical details. Below is an extended version of the route that demonstrates how to extract and display several of these attributes in a structured way. This can be especially useful when building applications that require dynamic localization, regional settings, or detailed geographic insights.
Route::get('/', function () {
$country = country('ae');
$countryFlag = $country->getFlag();
echo $countryFlag;
$countryName = "Name : " . $country->getName();
$countryNativeName = "Native name : " . $country->getNativeName();
$countryOfficialName = $country->getOfficialName();
$countryArea = 'Area : ' . $country->getArea();
$countyLanguages = $country->getLanguages();
$currencyDetails = $country->getCurrencies();
dd($countryName, $countryNativeName, $countryOfficialName, $countryArea, $countyLanguages, $currencyDetails);
});
This will output a range of useful details for the selected country, including its flag emoji, common name, native language name, official state name, total land area, list of spoken languages, and information about its currency or currencies. It’s an excellent starting point to understand the depth of data the rinvex/countries package provides and how you can leverage it to build location-aware features in your Laravel application.
Conclusion
By following this guide, you've successfully integrated the rinvex/countries package into your Laravel application to manage country data. You initialized a fresh Laravel project, installed the Rinvex Countries package, and set up routes to retrieve country lists and specific country details. You also explored predefined methods to access various attributes like country flags, names, languages, currencies, and geographical data. This powerful package provides an easy way to handle country information without maintaining your own dataset. Moving forward, you can integrate this functionality into features like dynamic localization, region-based settings, or user profiles.
For more details, please refer to the Rinvex Countries package documentation.
Share this with friends!
To engage in commentary, kindly proceed by logging in or registering
Subscribe to Our Newsletter
Stay ahead of the curve! Join our newsletter to see what everyone’s talking about.
0 Comments