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
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;
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();
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>
This is our result
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)
so after creating this country list, if we want to make relationships with it, how is it possible ? example creating parent child dropdowns
nigeria states doen't work for me but all it's okay