Hello guys, Today we are going to learn How to Seed US States in Database using Laravel 10 Seeder. When building applications that require geographical data, such as address forms or location-based features, it is crucial to have a comprehensive list of US states readily available in your database. In this tutorial, we will explore how to seed the US states into your Laravel 10 database using a seeder.
By following the steps outlined below, you will be able to quickly populate your database with the necessary information.
Prerequisites:
Before we begin, make sure you have Laravel 10 installed on your local development environment. Familiarity with Laravel’s database migrations and seeders is also beneficial.
Step 1: Creating a New Laravel 10 Project
First, open your terminal (or command prompt) and run the following command to create a new Laravel project:
composer create-project --prefer-dist laravel/laravel seed-us-states
Once the installation is complete, navigate to the project directory:
cd seed-us-states
Now, launch the Laravel development server using the following command:
php artisan serve
You can now access your new Laravel 10 project in your web browser at http://localhost:8000.
Step 2: Create the States Migration
First, let’s create a migration file to define the structure of the table that will hold the US states. In your terminal, run the following command:
php artisan make:migration create_states_table --create=states
This will generate a new migration file in the database/migrations
directory. Open the file and replace its contents with the following code:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStatesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('states', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('abbreviation');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('states');
}
}
Save the file and execute the migration using the following command:
php artisan migrate
Step 3: Create the States Seeder
Next, let’s create a seeder file to populate the states
table with the US states data. Run the following command in your terminal:
php artisan make:seeder StatesTableSeeder
This will generate a new seeder file in the database/seeders
directory. Open the file and replace its contents with the following code:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class StatesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$states = [
['name' => 'Alabama', 'abbreviation' => 'AL'],
['name' => 'Alaska', 'abbreviation' => 'AK'],
// Add more states here
];
DB::table('states')->insert($states);
}
}
In the $states
array, add more elements for each US state, providing the name and abbreviation. You can find a complete list of US states with their abbreviations from reliable sources.
Step 4: Run the Seeder
To execute the seeder and populate the states
table with the US states data, run the following command in your terminal:
php artisan db:seed --class=StatesTableSeeder
If the seeder executes successfully, you should see a message indicating the number of rows inserted into the table.
Step 5: Verify the Results
You can now verify that the US states have been seeded into the database. You can either query the states
table directly or create a route and view to display the list of states.
Conclusion:
In this tutorial, we have learned how to seed the US states into a Laravel 10 database using a seeder. By following the steps outlined above, you can easily populate your database with the necessary US states data. This will provide you with a convenient and reliable source of information when working with geographical data in your Laravel application.
Remember to keep your database migrations and seeders up to date as you add new features or modify existing data structures. Regularly updating the seeded data ensures that your application stays synchronized with any changes to the US states list.
By leveraging Laravel’s migration and seeder functionality, you can efficiently manage and maintain essential data in your application. This approach saves you time and effort by automating the process of populating your database with commonly used information.
We hope this tutorial has been helpful in guiding you through the process of seeding US states into your Laravel 10 database. Feel free to explore further and adapt the techniques to seed other types of data as per your application’s requirements.
Remember to refer to the official Laravel documentation for more in-depth information on migrations and seeders, as well as other essential features provided by the framework.
Happy coding!
[…] Also Read : How to Seed US States in Database using Laravel 10 Seeder […]
[…] Also Read: How to Seed US States in Database using Laravel 10 Seeder […]