Laravel US State Seeder Example

By Hardik Savani April 16, 2024 Category : Laravel

Today, laravel us state seeder example is our main topic. This article will give you simple example of laravel seeder for us states. Here you will learn laravel us states list. I’m going to show you about laravel us state list. Let's see bellow example how to get us state list in laravel.

Here, i will give you very simple example of how to add us states list using seeder in laravel. we will create table and it store on it. you can also use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

Let's see bellow steps:

Step 1: Install Laravel

first of all we need to get fresh Laravel 8 version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Seeder and US State Model

here, we will create migration for us_states table. so let's create migration as bellow:

php artisan make:migration create_us_states_table

database/migrations/your_migtion_file.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class CreateCountriesTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('us_states', function (Blueprint $table) {

$table->id();

$table->string('name');

$table->string('code');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('us_states');

}

}

now let's run migration:

php artisan migrate

next, add soft delete facade in user model as like bellow:

app/Models/USState.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class USState extends Model

{

use HasFactory;

protected $table = "us_states";

protected $fillable = [

'name', 'code'

];

}

Step 3: Create Seeder

In this step, we need to create add seeder for country lists.

Create Seeder with bellow command

php artisan make:seeder USStateSeeder

database/seeders/USStateSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

use App\Models\USState;

class USStateSeeder extends Seeder

{

/**

* Run the database seeds.

*

* @return void

*/

public function run()

{

USState::truncate();

$usSatates = [

["name" => "Alabama", "code" => "AL"],

["name" => "Alaska", "code" => "AK"],

["name" => "Arizona", "code" => "AZ"],

["name" => "Arkansas", "code" => "AR"],

["name" => "California", "code" => "CA"],

["name" => "Colorado", "code" => "CO"],

["name" => "Connecticut", "code" => "CT"],

["name" => "Delaware", "code" => "DE"],

["name" => "District of Columbia", "code" => "DC"],

["name" => "Florida", "code" => "FL"],

["name" => "Georgia", "code" => "GA"],

["name" => "Hawaii", "code" => "HI"],

["name" => "Idaho", "code" => "ID"],

["name" => "Illinois", "code" => "IL"],

["name" => "Indiana", "code" => "IN"],

["name" => "Iowa", "code" => "IA"],

["name" => "Kansas", "code" => "KS"],

["name" => "Kentucky", "code" => "KY"],

["name" => "Louisiana", "code" => "LA"],

["name" => "Maine", "code" => "ME"],

["name" => "Maryland", "code" => "MD"],

["name" => "Massachusetts", "code" => "MA"],

["name" => "Michigan", "code" => "MI"],

["name" => "Minnesota", "code" => "MN"],

["name" => "Mississippi", "code" => "MS"],

["name" => "Missouri", "code" => "MO"],

["name" => "Montana", "code" => "MT"],

["name" => "Nebraska", "code" => "NE"],

["name" => "Nevada", "code" => "NV"],

["name" => "New Hampshire", "code" => "NH"],

["name" => "New Jersey", "code" => "NJ"],

["name" => "New Mexico", "code" => "NM"],

["name" => "New York", "code" => "NY"],

["name" => "North Carolina", "code" => "NC"],

["name" => "North Dakota", "code" => "ND"],

["name" => "Ohio", "code" => "OH"],

["name" => "Oklahoma", "code" => "OK"],

["name" => "Oregon", "code" => "OR"],

["name" => "Pennsylvania", "code" => "PA"],

["name" => "Rhode Island", "code" => "RI"],

["name" => "South Carolina", "code" => "SC"],

["name" => "South Dakota", "code" => "SD"],

["name" => "Tennessee", "code" => "TN"],

["name" => "Texas", "code" => "TX"],

["name" => "Utah", "code" => "UT"],

["name" => "Vermont", "code" => "VT"],

["name" => "Virginia", "code" => "VA"],

["name" => "Washington", "code" => "WA"],

["name" => "West Virginia", "code" => "WV"],

["name" => "Wisconsin", "code" => "WI"],

["name" => "Wyoming", "code" => "WY"]

];

foreach ($usSatates as $key => $state) {

USState::create($state);

}

}

}

now let's run seeder:

php artisan db:seed --class=USStateSeeder

Now you can see records in table:

i hope it can help you...

Shares