Laravel 12 - Get Country Details with Rinvex.

Touseef Afridi
15 May 25

Laravel 12 - Get Country Details with Rinvex.

In this tutorial, you will learn how to use the Rinvex Countries package in Laravel 12 to get country details like name, ISO code, currency, and more for your location-based applications.


If you're a video person, feel free to skip the post and check out the video instead!


Quick Overview

In this guide, we'll walk you through integrating country data into your Laravel 12 application using the Rinvex Countries package. You'll begin by setting up a new Laravel project or using an existing one. After that, you'll install the rinvex/countries package to access a comprehensive list of countries with relevant details like names, ISO codes, currencies, and more. Next, we’ll define routes to retrieve and display country data, starting with a simple list of all countries. We’ll then refine the route to fetch data for a specific country, such as Germany, using its ISO 3166-1 alpha-2 code. Lastly, you'll test everything by running the Laravel development server and viewing the output in your browser. This setup provides a foundation for building applications with dynamic country selection features and more.

Step # 1 : Set Up a New Laravel 12 Project.

Begin by launching a new Laravel 12 application or continue with an existing one. To start fresh, If you have Laravel globally installed you can use the following command.
laravel new countries
Alternatively, if you prefer using Composer directly, you can run.
composer create-project laravel/laravel --prefer-dist countries
During the interactive setup process, Laravel will prompt you to configure a few options.
  • Starter Kit : Choose None to start with a clean installation, without any pre-built authentication features.
  • Database Driver : Select MySQL for managing your application's data.
  • Run Migrations : Type yes to create the default database tables, such as users and password_reset_tokens.
  • Frontend Build : When asked if you want to run npm install and npm run build, choose yes to compile the frontend assets right away.

After completing these steps, a new Laravel 12 application named countries will be ready on your local environment. It will include the default Laravel structure, configured to use MySQL. This setup provides a clean foundation for building scalable and modern web applications with Laravel 12.

Step # 2 : Open Your Laravel Project Folder.

Once your Laravel project is set up, open your terminal (like Git Bash, CMD, or Terminal on macOS/Linux) and navigate into the project directory. This is where you'll run most of your Laravel commands. For example, in Git Bash, run
cd c:xampp/htdocs/countries
Make sure to update the path based on where you saved your Laravel project. Being inside the project folder ensures that Artisan and other Laravel tools work correctly.

Step # 3 : Install the Rinvex Countries and Data Package.

To simplify handling country-related data, install the rinvex/countries package. This package offers a preloaded list of countries along with details like names, ISO codes, dial codes, currencies, timezones, and more, ideal for applications that need a dynamic country dropdown or geographical filtering.
composer require rinvex/countries
Once installed, the package is automatically registered through Laravel’s package discovery. You can start accessing country data via helper methods or by publishing and customizing its resources. This saves time and ensures you're working with accurate, up-to-date international data.

Step # 4 : Define a Route to Retrieve All Countries.

Next, let's create a simple route to fetch and display all available countries using the countries() helper function. Open the web.php file and add this route.
Route::get('/', function () {
 $countries = countries();
 dd($countries);
});
This route will be triggered when you visit the root URL (/) of your app. It retrieves the full list of country data and uses dd() to quickly dump the information to the screen. This helps confirm that the package is working and returning the expected country data.

Step # 5 : It's time to test.

Now, it’s time to see everything in action. Start the Laravel development server by running.
php artisan serve
Once the server is up and running, open your browser and go to: 127.0.0.1:8000. You should see the list of country data dumped on the screen, confirming that the Rinvex package is working properly and the route is returning the expected results. This is a simple way to ensure that your setup is correct before moving on to further development.

Now, let’s refine our route to fetch details for a specific country using its country code, such as the ISO 3166-1 alpha-2 code. For example, to get details for Germany, update the route like this.
Route::get('/', function () {
    $country = country('de'); // ISO code for Germany
    dd($country);
});
Now, when you visit the root URL (/), it will retrieve and display the data for Germany, including its name, code, timezone, and other relevant details. This allows you to fetch specific country information based on the country code, making it easy to display data for any country you need.

Laravel’s Rinvex package provides several predefined methods to retrieve specific details about a country. Let’s update the route to fetch and display detailed information for Germany. Here’s how you can update your route.
Route::get('/', function () {
    $country = country('de'); // Fetch data for Germany
    $countryFlag = $country->getFlag();
    echo $countryFlag;
    $countryName = "Name: " . $country->getName();
    $countryNativeName = "Native Name: " . $country->getNativeName();
    $countryOfficialName = $country->getOfficialName();
    $countryArea = 'Area: ' . $country->getArea();
    $countryLanguages = $country->getLanguages();
    $currencyDetails = $country->getCurrencies();
    dd($countryName, $countryNativeName, $countryOfficialName, $countryArea, $countryLanguages, $currencyDetails);
});
In this updated route, we’re using several predefined methods from the Rinvex package to fetch key information about Germany, including the flag, name, native name, official name, area, languages, and currency details. When you visit the root URL (/), this data will be displayed on the screen, providing a comprehensive view of Germany’s details. This step demonstrates how easily you can retrieve specific country data using the built-in methods of the country() helper.

Conclusion

By following this guide, you've successfully integrated the Rinvex Countries package into your Laravel 12 application. Starting from setting up a fresh project, you installed the package and explored its features to retrieve comprehensive country data. You then defined routes to fetch and display country details, including using ISO country codes to get specific country information like names, flags, languages, and currencies. After testing the setup by running the Laravel development server, you verified that the package works as expected. This setup provides a solid foundation for building location-aware features such as dynamic dropdowns or forms based on country data. You can now expand this functionality to build more advanced features, enhancing the user experience with geographical data.
For additional details and advanced use cases, refer to the Rinvex Countries package documentation.

Share this with friends!


"Give this post some love and slap that 💖 button as if it owes you money! 💸😄"
0

0 Comments

To engage in commentary, kindly proceed by logging in or registering