earmsby's avatar

Countries for Laravel 11/12

I'm looking to add a table of countries to a project I'm building. For testing, i just made a migration and added a handful of countries manually. I imagine there's something out there that I could just import into my project that would add all the countries of the world. I don't need anything fancy like currencies and languages - just a list of countries with ISO codes. Any suggestions?

0 likes
7 replies
LaryAI's avatar
Level 58

To add a list of countries with ISO codes to your Laravel project, you can use a package or seed your database with a predefined list. Here's a simple approach using a seeder:

  1. Create a Migration for the Countries Table:

    First, create a migration for the countries table:

    php artisan make:migration create_countries_table
    

    Then, define the schema in the migration file:

    Schema::create('countries', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('iso_code', 3);
        $table->timestamps();
    });
    
  2. Create a Seeder for the Countries:

    Create a seeder to populate the countries table:

    php artisan make:seeder CountriesTableSeeder
    

    In the CountriesTableSeeder file, you can use a predefined list of countries with their ISO codes. Here's an example:

    use Illuminate\Database\Seeder;
    use Illuminate\Support\Facades\DB;
    
    class CountriesTableSeeder extends Seeder
    {
        public function run()
        {
            $countries = [
                ['name' => 'United States', 'iso_code' => 'US'],
                ['name' => 'Canada', 'iso_code' => 'CA'],
                ['name' => 'United Kingdom', 'iso_code' => 'GB'],
                // Add more countries as needed
            ];
    
            DB::table('countries')->insert($countries);
        }
    }
    
  3. Run the Migrations and Seeders:

    Run the migration and seeder to create the table and populate it with data:

    php artisan migrate
    php artisan db:seed --class=CountriesTableSeeder
    
  4. Optional: Use a Package

    If you prefer using a package, you can consider using webpatser/laravel-countries, which provides a comprehensive list of countries with ISO codes. You can install it via Composer:

    composer require webpatser/laravel-countries
    

    Then, publish the configuration and run the migration provided by the package:

    php artisan vendor:publish --provider="Webpatser\Countries\CountriesServiceProvider"
    php artisan migrate
    

    This package will automatically seed the countries table with a complete list of countries and their ISO codes.

By following these steps, you can easily add a list of countries with ISO codes to your Laravel project.

earmsby's avatar

That was sort of helpful. The first part about seeding the table is essentially what I already did. I'm hoping to find a source for all countries of the world without having to add them all manually (either in a seeder or directly in the table). The second part is what I was looking for but this package seems pretty old. I was able to install the package but nothing past that. I just got a message 'No publishable resources for tag [].' when I tried to publish the configuration. I'll keep looking, I guess.

bvfi-dev's avatar

Use ChatGPT, depending on your subscription. Ive found it to be very good for dumb work, especially making seeders.

So, you can start a chat with a small sample and then ask it to fill it fully out for you in a file. It would build a file using python and then send you a complete custom DB seeder. You can even attach it files in the chat to use as references, if you want something specific.

Then what you should also do is start a new chat, send the previously generated countries file (From the prev ChatGPT chat) and ask the new ChatGPT to proof it and tell it theres countries missing and to find out which ones, and it will actually double check. Try to fool it, and never give in on a first attempt, cos it can make mistakes, so you need to ask it to double check by fooling it.

1 like
maned3v's avatar

Create a seeder with ChatGPT, just ask him to do it. Do not forget to specify the fields you need. I've done it several times, it's very useful for these things

earmsby's avatar

Thanks for the suggestion of ChatGPT. I hadn't thought of that but tried it. It took a couple tries, but in the end I got a good table that suits my needs. Much simpler than the package I had tried initially.

Please or to participate in this conversation.