Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

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.

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)

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.

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

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

$ php artisan make:model Country -m

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

$ php artisan make:model State -m

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

$ php artisan make:model City -m

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)

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)

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)

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

$ php artisan migrate

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

$ 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)

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

What is CodeIgniter and use cases of CodeIgniter?

What is CodeIgniter? CodeIgniter is an open-source PHP web application framework designed to simplify and accelerate web development. It follows the Model-View-Controller (MVC) architectural pattern and provides…

Read More

What is PHP and use cases of PHP?

What is PHP? Are you wondering what PHP is and how it works? Look no further! In this article, we will explore the ins and outs of…

Read More

Complete guide on PHP certification courses, tutorials & training

What is PHP Hypertext Preprocessor is known as PHP. Many developers use PHP, an open-source server-side programming language, to create websites. In addition, it is a general-purpose…

Read More

Complete PHP with Laravel Certification Guide & tutorials

What is PHP with Laravel? PHP was visioned eventually in the fall of 1994 by Rasmus Lerdorf. PHP means Hypertext Preprocessor. It’s a extensively- used, open source…

Read More

Top 50 PHP Interview Questions and Answers

1) What is PHP? PHP is an open-source, interpreted, and object-oriented scripting language that can be executed at the server-side. PHP is well suited for web development….

Read More

Complete guide of PHP certification courses, tutorials & training

PHP is a great beginner programming language for anyone who is wanting to cross over into the coding world. Though easy to learn, it is an extremely…

Read More