Laravel Country List with Flags Example

By Hardik Savani November 5, 2023 Category : Laravel

Hello Friends,

In this guide, we are going to learn laravel country list with flags. I would like to show you list countries with flags laravel. I explained simply step by step how to add country list with flag in laravel. you can see laravel 10 country with flags example. follow the below step for laravel country list with flags.

In this post, we will use stidges/laravel-country-flags composer package to get country list with flags. we will create countries table with name and code column. then we will add some dummy data to that table and display it.

So, let's follow the below step to done this example.

Preview:

Step 1: Install Laravel

This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Country Table and Model

Here, we need to create a database migration for the "countries" table and also we will create a model for the "countries" table.

php artisan make:migration create_countries_table

Migration:

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*/

public function up(): void

{

Schema::create('countries', function (Blueprint $table) {

$table->id();

$table->string('name');

$table->string('code');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::dropIfExists('countries');

}

};

Now, let's run migration command to create table:

php artisan migrate

Next, we need to create country model using following command:

php artisan make:model Country

You can update like as the below:

app/Models/Country.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class Country extends Model

{

use HasFactory;

}

Step 3: Install stidges/laravel-country-flags Package

Here, we will install stidges/laravel-country-flags composer package to get country flags from code. so, let's run following command to install composer package.

composer require stidges/laravel-country-flags

Step 4: Create and Add Routes

Furthermore, open routes/web.php file and add the routes to manage GET render view.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\CountryController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('countries', [CountryController::class, 'index']);

Step 5: Create Controller

In this step, we will create a new CountryController; in this file, we will add two method index() for render view.

next, let's update the following code to Controller File.

app/Http/Controllers/CountryController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Country;

class CountryController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$countries = Country::all();

return view('countryList', compact('countries'));

}

}

Step 6: Create Blade File

At last step we need to create countryList.blade.php file and in this file we will create form with file input button. So copy bellow and put on that file.

resources/views/countryList.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Country List with Flags Example</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container">

<h2>Laravel Country List with Flags Example - ItSolutionStuff.com</h2>

<table class="table table-bordered">

<tr>

<th>Image</th>

<th>Name</th>

</tr>

@foreach ($countries as $country)

<tr>

<td>{{ country_flag($country->code) }}</td>

<td>{{ $country->name }}</td>

</tr>

@endforeach

</table>

</div>

</body>

</html>

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/countries

you need to add some dummy records on your table like as the below:

I hope it can help you...

Tags :
Shares