<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsStatesTable 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');
}
}
<?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',
];
}
Laravel US State Seeder Example
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 and laravel 9 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' ];}
By Hardik Savani August 11, 2021 Category : LaravelPlayUnmuteLoaded: 1.15%FullscreenToday, 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.