Chat 2024-10-19 15:13
October 19, 2024 ยท AI Conversations
### Step 1: Create a Migration for the `countries` Table
Run the following Artisan command to generate a migration:
php artisan make:migration create_countries_table
This will create a migration file in the `database/migrations` directory. Open that file and modify the `up` method like this:
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('iso_code', 3)->unique();
$table->string('phone_code', 10);
$table->timestamps();
});
}
This will create a table with the following columns:
– `name`: The name of the country.
– `iso_code`: The ISO 3166-1 alpha-3 country code.
– `phone_code`: The international calling code.
### Step 2: Create a Seeder to Insert Country Data
You can use a seeder to insert all countries into the `countries` table. Run the following Artisan command to generate a seeder:
php artisan make:seeder CountriesSeeder
Open the `CountriesSeeder.php` file in the `database/seeders` directory and add the country data like this:
use IlluminateDatabaseSeeder;
use IlluminateSupportFacadesDB;
class CountriesSeeder extends Seeder
{
public function run()
{
$countries = [
['name' => 'Afghanistan', 'iso_code' => 'AFG', 'phone_code' => '93'],
['name' => 'Albania', 'iso_code' => 'ALB', 'phone_code' => '355'],
['name' => 'Algeria', 'iso_code' => 'DZA', 'phone_code' => '213'],
// Add all other countries here
];
DB::table('countries')->insert($countries);
}
}
You can find the full list of countries with their ISO 3166-1 codes and phone codes online, and you can populate the `$countries` array accordingly.
### Step 3: Run the Migration and Seeder
After creating the migration and seeder, you can run the migration and seed the database:
php artisan migrate
php artisan db:seed --class=CountriesSeeder
This will create the `countries` table and populate it with the countries' data.
Let me know if you'd like help with the complete list of countries and codes!