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 show City List as per Country and State select in DropDown using AJAX in Laravel? Part-1

How to use AJAX in DropDown in Laravel? Example as in Country, State, and City.

What is the DropDown?

Create a dropdown box that appears when the user moves the mouse over an element.

To know more https://www.w3schools.com/css/css_dropdowns.asp.

Step 1. We need to Create a Database for this and Go to XAMPP server->phpMyAdmin->Click New Database-> country_state_city.

PhpMyAdmin Create database form with country_state_city entered as the new database name

Step 2. To Create a new project, so open xampp/htdocs folder->Right Click-> Open GitBash -> Write following Command:-

$ composer create-project --prefer-dist laravel/laravel country_sate_city "5.8.*"Code language: JavaScript (javascript)
Git Bash running composer create-project for Laravel 5.8 project country_state_city

Mysql Database connection Laravel

Step 3. Thereafter, Go to .env file to set the project path and give the project DB_DATABASE name and DB_USERNAME name.

Sublime Text .env file with APP_URL set to localhost/country_state_city/public highlighted
Sublime Text .env file with DB_DATABASE=country_state_city and DB_USERNAME=root highlighted

Step 4. Now, Go to config/database.php give the project DB_DATABASE name and DB_USERNAME name.

Config/database.php mysql settings with database country_state_city and username root highlighted

Step 5. To Create the first model for creating a table, Write the following command.

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

Step 6. Similarly, To Create a second model for creating a table, Write the following command.

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

Step 7. Likewise, To Create the third model for creating a table, Write the following command:

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

Step 8. To Generate a migration file in the database/migrations folder of Country table.

public function up()
    {
        Schema::create('countries', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('country_name');
            $table->timestamps();
        });
    }Code language: PHP (php)
Create_countries_table migration up method with id, country_name and timestamps columns

Step 9. To Generate a migration file in the database/migrations folder of State table.

 public function up()
    {
        Schema::create('states', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('state_name');
            $table->integer('country_id');
            $table->timestamps();
        });
    }Code language: PHP (php)
Create_states_table migration up method with id, state_name, country_id and timestamps columns

Step 10. To Generate migration file in the database/migrations folder of the City table.

public function up()
    {
        Schema::create('cities', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('city_name');
            $table->integer('state_id');
            $table->timestamps();
        });
    }Code language: PHP (php)
Sublime Text showing the create_cities_table migration with city_name and state_id columns in up()

Step 11. To migrate these tables into the MySQL database, so write the following command.

$ php artisan migrate
Git Bash terminal running php artisan migrate in the country_state_city Laravel project

Step 12. To Create a Controller in App/Http/Controller folder so, write the following command.

$ php artisan make:controller MainController --resource
Git Bash terminal running php artisan make:controller MainController --resource

Step 13. So, Go to the app/Http/Controllers/MainController.php file and write the following codes within the index(), getStates(), and getCities() functions.

public function index(){
    	$countries = Country::all()->pluck('country_name','id');
    	return view('welcome',compact('countries'));
    }Code language: PHP (php)
 public function getStates($id){
    	$states= State::where('country_id',$id)->pluck('state_name','id');
        return json_encode($states);
    }Code language: PHP (php)
public function getCities($id){
    	$cities= City::where('state_id',$id)->pluck('city_name','id');
        return json_encode($cities);
    }Code language: PHP (php)

Step 14: Go to routes/web.php file and define the route of these all MainController functions.

Route::get('/','MainController@index');
Route::get('/getStates/{id}','MainController@getStates');
Route::get('/getCities/{id}','MainController@getCities');Code language: PHP (php)
Laravel routes/web.php defining routes for MainController index, getStates and getCities

CRUD in LARAVEL blog

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

Simple Ajax CRUD Application in Laravel

Step 1 – Create a fresh laravel projet First off all , create a project using following command given below – Change Directory to AjaxCrud folder Step…

Read More

CRUD Operations in Laravel PHP Framework

Step 1 – Download Laravel 5.8 In first step you have to download Laravel 5.8 version. For this you have to go to command prompt, in which…

Read More

How to Create a Simple Login Authentication System in LARAVEL?Part-1

How to Create a Simple Login Authentication System in LARAVEL? Firstly, we’ll create a new project for this. Step 1. Go to XAMPP/htdocs folder -> right Click->Open Git…

Read More

Todo Application in Laravel

1.Create Project in Laravel Composer create-project laravel/laravel todolist 2.Create CRUD Controller php artisan make:controller TodosController –resource 3.Route setup of CRUD Controller routes/web.php 4.Create Database todolist localhost/phpmyadmin create…

Read More

Full Working Example — Paytm Integration in Laravel

I’ll give you a full working Paytm Payment Gateway integration example in Laravel — step-by-step, including payment page, initiate payment, callback handling, and success/failure handling. 🚀 Full…

Read More

What is Ajax and use cases of Ajax?

What is Ajax? Ajax is a set of web development techniques that allows web pages to be updated asynchronously by exchanging small amounts of data with the…

Read More