Laravel 7.x and 6.x country list using Countries package

November 17, 2017 | Category : Laravel 5.5 Laravel 5 Laravel PHP

Hi Guys.

In this article i will let you know about countries dropdown in laravel. We may sometime need to get all countries with flag, currency, states, languages, code, timezone, capital, calling code, sign, area, latitude and logitude, multiple ISO codes etc. So It is not better if you create database table with country and then manually you store all information from google or something else.

So it would be great if you use composer page for countries details. So you don't require to add manually all then country details and from research and it can save your time. If you are using laravel then you can use antonioribeiro/countries composer package.

antonioribeiro/countries package give you Countries facade and you can simply use like model. You can easily get all details of country like flag, currency, states, languages, code, timezone, capital, calling code, sign, area, latitude and logitude, geometry, ISO codes, translations etc.

Here i gave you very simple example for get all countries with it's flag, it's very simple and small example but perfect. So you have to just follow few things.

Step 1: Install antonioribeiro/countries Composer Package

First of all, we need to install antonioribeiro/countries package for get Countries facade and using that facade we can get all information about specific country. So simply run bellow composer command for install package.

composer require pragmarx/countries

After install successfully above antonioribeiro/countries package we need to add into providers and aliases array of configuration file. So let's add following way:

config/app.php

<?php

return [

....

'providers' => [

....

PragmaRX\Countries\ServiceProvider::class,

],

'aliases' => [

....

'Countries'=> PragmaRX\Countries\Facade::class,

]

Step 2: Create Web Route

Here, we need to add new route for display countries with flag and it will target HomeController countries(). So let's add following route:

routes/web.php

Route::get('countries', 'HomeController@countries');

Step 3: Create Controller Method

In Third step, we will add countries() on HomeController. So there we will write code for get all details of country and return view, so add bellow method on HomeController:

app/Http/Controllers/HomeController.php

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use Countries;


class HomeController extends Controller

{

public function countries()

{

$countries = Countries::all();

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

}

}

Step 4: create blade file

Now at the last, we require to create countries.blade.php file. In this file we will display name of country and it's flag. So let's create blade file and put bellow code:

resources/views/countries.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Countries Lists</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/2.8.0/css/flag-icon.min.css" />

</head>

<body>


<div class="container">

<h2 class="text-center">Laravel 5.5 Countries Lists</h2>


@if($countries->count())

@foreach($countries as $country)

<span style="padding: 5px;"> {!! $country->flag['flag-icon'] !!} {!! $country->name->common !!} </span>

@endforeach

@endif

</div>


</body>

</html>

you will find more information about "antonioribeiro/countries" from here : antonioribeiro/countries.

I hope you found your best solution...