DEV Community

Kingsconsult
Kingsconsult

Posted on • Updated on

Country list in laravel 8 and Laravel 7

Hello guys, today I am going to show you how to get the list of all the countries in your laravel 8 app, it is also applicable to laravel 7 downwards.
Sometimes, you might need to create a form where the user is required to add his/her country or you just need to use the list of all the countries, you don’t need to ask your user to type his/her country or searching online for the list of all the countries, all you need is call a class and it will give you an array of all the countries in the world.

Click on my profile to follow me to get more updates.

This is simple using a package called Monarobase/country-list, without saying much, let’s dive in

Step 1: add the package to our app

composer require monarobase/country-list

install monarobase

Step 2: add the ServiceProvider to the providers

Go to config/app.php and locate the array for Providers, add the class below

Monarobase\CountryList\CountryListServiceProvider::class,

also add the code below to the aliases

'Countries' => Monarobase\CountryList\CountryListFacade::class,

Step 3: call the class in our controller

At the top of the controller that you are going to use, call the class after the namespace.

use Monarobase\CountryList\CountryListFacade;

use Monarobase class in controller

Step 4: declare a variable

Go to the method that you want to use, declare a variable with the class. You will also send it to the view using compact();
declare and send the variable to view

Step 5: use the variable in your view



<div class="col-xs-12 col-sm-12 col-md-12">
    <div class="form-group">
        <strong>Country:</strong>
        <select name="country" id="country" class="form-control mb-1">
            <option value="">Select Country</option>
            @foreach ($countries as $country)
                <option value="{{ $country }}">{{ $country }}</option>
            @endforeach
        </select>
    </div>
</div>


Enter fullscreen mode Exit fullscreen mode

This is our result
a form with country list
as usual, You can follow me, leave a comment below, suggestion, reaction, or email me.
Click on my profile, to see my other post and contacts

Top comments (2)

Collapse
 
mmmsabeel profile image
Mohammed Sabeel

so after creating this country list, if we want to make relationships with it, how is it possible ? example creating parent child dropdowns

Collapse
 
dani03 profile image
dani03

nigeria states doen't work for me but all it's okay