How to Store Countries, States, Cities seed classes into the Database in Laravel PHP

Step 1. Create a City seeder file. Write down the following command as follows:
$ php artisan make:seeder CitiesTableSeeder

Step 2. Create a City model. Write down the following command as follows:
$ php artisan make:model City -m

Step 3. Add columns into Country table.
public function up()
{
Schema::create('cities', function (Blueprint $table) {
$table->bigIncrements('city_id');
$table->string('city_name');
$table->integer('state_id');
$table->timestamps();
});
}
Step 4. Go to Database/seeds/DatabaseSeeder file. Write down the following command as follows to add City seeder class:
$this->call(CitiesTableSeeder::class);
public function run()
{
$this->call(CountriesTableSeeder::class);
$this->call(StatesTableSeeder::class);
$this->call(CitiesTableSeeder::class);
}
Step 5. Go to Database/seeds/CitiesTableSeeder 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=CitiesTableSeeder
Step 8. Go to app\http\Controllers\MainController.php file. Define funcitions of City.
public function getCities($id){
$cities= City::where('state_id',$id)->get();
return response()->json(['cities' => $cities]);
}
Step 9. Then, go to routes/web.php file and define all these routes.
Route::get('/getCities/{id}','MainController@getCities');

Step 10. Go to resources/views/welcome.blade.php file


Thanks



With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.