How to Store Countries, States, Cities seed classes into the Database in Laravel PHP?
Step 1. Create a State seeder file. Write down the following command as follows:
$ php artisan make:seeder StatesTableSeeder
Step 2. Create a State model. Write down the following command as follows:
$ php artisan make:model State -m
Step 3. Add columns into Country table.
public function up()
{
Schema::create('states', function (Blueprint $table) {
$table->bigIncrements('state_id');
$table->string('state_name');
$table->integer('country_id');
$table->timestamps();
});
}
Step 4. Go to Database/seeds/DatabaseSeeder file. Write down the following command as follows to add State seeder class:
public function run()
{
$this->call(CountriesTableSeeder::class);
$this->call(StatesTableSeeder::class);
}
Step 5. Go to Database/seeds/CountriesTableSeeder file.
Step 6. Migrate the tables into the MySQL database.
$ php artisan migrate
Step 7. Run the seeder class file individually.
$ php artisan db:seed --class=StatesTableSeeder
Step 8. Go to app\http\Controllers\MainController.php file. Define funcitions of Country and State.
Step 9. Then, go to routes/web.php file and define all these routes.
Route::get('/getStates/{id}','MainController@getStates');
Thanks
- How to Login with Token in Laravel PHP Framework? - October 30, 2021
- How to merge two or multiple tables to each other in the Laravel PHP Framework? (Part-4) - October 29, 2021
- How to display a table in a Verticle or Horizontal form in the Laravel PHP Framework? Part-2 - October 29, 2021