Laravel 11 - Blade Flags

Touseef Afridi
02 Oct 24

Laravel 11 - Blade Flags

This tutorial covers displaying country flags using the Blade flags package, which is useful for easily incorporating country visuals into your Laravel application.


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


Quick Overview

This guide walks through the process of integrating country flags into a Laravel project using the Blade Flags package. It starts by setting up a Laravel project, installing the required package, and publishing its configuration. Next, the view file is updated to display flags within a table using Blade components. The guide also covers different ways to style flags using Tailwind CSS classes, inline styles, or dynamic rendering based on country codes. Finally, the Laravel development server is started, and the implementation is tested in the browser to ensure the flags display correctly.

Step # 1 : Create fresh Laravel project or use existing project.

If Laravel is installed globally, create a new project by running.
laravel new flags
Or
Alternatively, use Composer to create the fresh project.
composer create-project laravel/laravel --prefer-dist flags
During the setup, choose the following options when prompted to ensure a smooth installation process tailored to your application's needs.
  • Would you like to install the starter kit? β€” Choose None to keep the installation minimal.
  • Testing framework β€” Choose Pest for a modern and expressive testing experience.
  • Database for your application β€” Choose MySQL for a robust and widely used database solution.
  • Run the default database migration β€” Type Yes to set up the database structure immediately.

This step sets up a Laravel project using either the Laravel CLI or Composer. It ensures that no authentication starter kit is pre-installed, Pest is selected for testing, MySQL is configured as the database, and default migrations are applied.

Step # 2 : Navigate to the Project Directory.

Open a terminal (e.g., Git Bash) and navigate to your Laravel project's root folder.
cd c:xampp/htdocs/flags
Once inside, you're set to run the next commands.

Step # 3 : Install the Blade Flags Package.

Run the following command to add the package to your Laravel project.
composer require outhebox/blade-flags
This installs the outhebox/blade-flags package, enabling you to easily display country flags in your Blade templates using a simple and intuitive syntax. The package provides a collection of SVG-based flags that can be rendered using Blade components, ensuring lightweight and high-quality visuals. It supports dynamic flag rendering based on country codes, making it useful for multilingual applications, e-commerce platforms, and any project requiring international representation. Additionally, Blade Flags integrates seamlessly with Tailwind CSS and other styling approaches, allowing you to customize flag appearance while maintaining responsiveness and consistency across different screen sizes.

Step # 4 : Publish the configuration.

Run the following command to publish the package configuration.
php artisan vendor:publish --tag=blade-flags-config
This command publishes the blade-flags.php configuration file, allowing you to customize the package settings according to your application's needs. By modifying this file, you can control various aspects of how flags are rendered, such as default flag styles, available flag sets, and potential caching options to improve performance. This flexibility ensures that the package can be adapted to different design requirements, whether you're working on a multilingual website, an admin dashboard, or a user profile system that displays country flags dynamically.

Step # 5 : Update the View.

Modify the welcome.blade.php file to display a country flag by adding the following HTML.
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Laravel Country Flags - Code Shotcut</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
  <div class="max-w-6xl mx-auto py-8 px-4">
    <h2 class="text-2xl font-bold mb-6">Country Flag Example - All About Laravel</h2>
    <table class="min-w-full border border-gray-300">
      <thead>
        <tr class="bg-gray-100">
          <th class="border px-4 py-2 text-left">Image</th>
          <th class="border px-4 py-2 text-left">Name</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="border px-4 py-2"><x-flag-country-ae/></td>
          <td class="border px-4 py-2">United Arab Emirates</td>
        </tr>
      </tbody>
    </table>
  </div>
</body>
</html>
This updates the welcome.blade.php file to display country flags using the Blade Flags package. The table structure, styled with Tailwind CSS, includes a column for flag images and another for country names. The <x-flag-country-ae/> Blade component dynamically renders the UAE flag, which can be changed by using a different country code. You can further enhance this setup by dynamically retrieving country codes from a database, allowing users to select a country and display its corresponding flag. Additionally, applying Tailwind CSS classes ensures better styling, spacing, and alignment, making your application more interactive and adaptable for internationalization or region-specific content display.

Step # 6 : It's time to test.

Start the Laravel development server by running the following command.
php artisan serve
Once the server is running, open your browser and visit.
127.0.0.1:8000

You can control the flag size using CSS classes, making it easy to maintain a uniform appearance across your application. Update the table body as follows.
      <tbody>
        <tr>
          <td class="border px-4 py-2 w-6"><x-flag-country-ae/></td>
          <td class="border px-4 py-2">United Arab Emirates</td>
        </tr>
      </tbody>

Alternatively, you can set the flag size using inline styles for more precise control over its dimensions and positioning within the layout.
      <tbody>
        <tr>
          <td class="border px-4 py-2" style="width: 200px;"><x-flag-country-ae/></td>
          <td class="border px-4 py-2">United Arab Emirates</td>
        </tr>
      </tbody>

To display country flags dynamically based on the iso2_code, update the table body like this to ensure flexibility in rendering different country flags without manually specifying each one.
      <tbody>
        <tr>
         <!-- Pass the dynamic country iso2_code in place of {{ 'DE' }} -->
         <td class="border px-4 py-2"><x-icon name="flag-country-{{ 'DE' }}" /></td>
         <td class="border px-4 py-2">Germany</td>
       </tr>
     </tbody>

You can further enhance the flag display by combining Tailwind CSS classes with conditional rendering to adapt the UI dynamically. Whether you choose predefined sizes, inline styles, or dynamic country codes, the Blade Flags package offers a scalable and flexible way to integrate country flags into your Laravel application. This makes it ideal for projects requiring multilingual support, country-based content, or location-aware features. By leveraging this package, you ensure a visually appealing and efficient solution for flag representation across different sections of your website.

Conclusion

By following this guide, you've successfully integrated country flags into your Laravel application using the Blade Flags package. You can now display flags dynamically in your views, style them using Tailwind CSS or inline styles, and render them based on country codes for flexibility. This implementation enhances the user interface by providing a visual representation of countries within tables or other components. You can further customize the flag display by adjusting styles, integrating them into different sections of your application, or using additional features from the Blade Flags package.
For more details, refer to the Blade Flags 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