MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to Store Countries, States, Cities seed classes into the Database in Laravel PHP? (Part-3)

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

Part-1 Part- 2

Laravel DropDown Feature page with empty Select Country, Select State and Select City dropdowns

Step 1. Create a City seeder file. Write down the following command as follows:

$  php artisan make:seeder CitiesTableSeeder
Git Bash terminal running php artisan make:seeder CitiesTableSeeder with Seeder created successfully

Step 2. Create a City model. Write down the following command as follows:

$  php artisan make:model City -m
Git Bash terminal running php artisan make:model City -m in the country_state_city project

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();
        });
    }Code language: PHP (php)

Step 4. Go to Database/seeds/DatabaseSeeder file. Write down the following command as follows to add City seeder class:

$this->call(CitiesTableSeeder::class);
Code language: JavaScript (javascript)
public function run()
    {
        $this->call(CountriesTableSeeder::class);
        $this->call(StatesTableSeeder::class);
        $this->call(CitiesTableSeeder::class);
    }Code language: PHP (php)

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=CitiesTableSeederCode language: JavaScript (javascript)

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]);
   }Code language: PHP (php)

Step 9. Then, go to routes/web.php file and define all these routes.

Route::get('/getCities/{id}','MainController@getCities');Code language: JavaScript (javascript)
Sublime Text showing routes/web.php with index, getStates and getCities routes for MainController

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

City dropdown open with a list of cities and Bokaro Steel City highlighted
DropDown Feature form with India, Jharkhand and Bokaro Steel City selected

Thanks

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
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.

Related Posts

How to display a table in a Verticle or Horizontal form in the Laravel PHP Framework? Part-1

How to display a table in a Verticle or Horizontal form in the Laravel PHP Framework? How to Store Seed file into Database. Click There are two…

Read More

HOW TO CREATE EVENT CALENDAR IN LARAVEL? PART-3

Step 1.Create a Database in phpMyAdmin. (Write the same name of database which was written in .enf databse name). Step 2.  We use the migrate command to…

Read More

HOW TO CREATE EVENT CALENDAR IN LARAVEL? PART-2

Step 1.Create a Laravel Project Firstly, Create a new project in laravel, so we open Git Bash,  Go to My Computer/This PC–>LocalDisk(C:)–>Xampp–>Htdocs (Press right click on Mouse)…

Read More

How to file upload in DB With local folder using laravel 5.8

Step:1 you have to make these(A) – php artisan make:controller ImageUploadController(B) – php artisan make:model ImageUpload -m Step:2 Open the welcome.blade.php page then write this code. Step:3…

Read More

How to Store Countries, States, Cities seed classes into the Database in Laravel PHP? (Part-1)

How to Store Countries, States, Cities seed classes into the Database in Laravel PHP? Part- 2 Part- 3 Step 1. Create a Country seeder file. Write down…

Read More

How to Store Countries, States, Cities seed classes into the Database in Laravel PHP? (Part-2)

How to Store Countries, States, Cities seed classes into the Database in Laravel PHP? Part-1 Part-3 Step 1. Create a State seeder file. Write down the following…

Read More