[email protected] Chirags Laravel Tutorial https://www.chirags.in
*********************************************************************************************
Laravel Dependent AJAX Dropdown Country State City from MySQL Database
***********************************************************************************************
#Create Project in Laravel
composer create-project --prefer-dist laravel/laravel Laravel-Dependent-AJAX-Dropdown-CountryStateCity
#Add Database Details in .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laraveldropdowncountrystatecity
DB_USERNAME=root
DB_PASSWORD=
#Create Model and Run Migration
php artisan make:model Country
php artisan make:model State
php artisan make:model City
php artisan make:migration create_country_state_city_tables
#migrations/create_country_state_city_tables.php file and update the following code:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCountryStateCityTables extends Migration
{
/**
Run the migrations.
*
@return void
*/
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table➡increments('country_id');
$table➡string('country_name');
$table➡timestamps();
});
Schema::create('states', function (Blueprint $table) {
$table➡increments('state_id');
$table➡string('state_name');
$table➡integer('country_id');
$table➡timestamps();
});
Schema::create('cities', function (Blueprint $table) {
$table➡increments('city_id');
$table➡string('city_name');
$table➡integer('state_id');
$table➡timestamps();
});
}
/**
Reverse the migrations.
*
@return void
*/
public function down()
{
Schema::drop('countries');
Schema::drop('states');
Schema::drop('cities');
}
}
php artisan migrate
#Prepare Controller
php artisan make:controller DropdownController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\{Country, State, City};
class DropdownController extends Controller
{
public function index()
{
$data['countries'] = Country::get(["country_id","country_name"]);
return view('index', $data);
}
public function getStates(Request $request)
{
$data['states'] = State::where("country_id",$request➡country_id)➡get(["state_id","state_name"]);
return response()➡json($data);
}
public function getCities(Request $request)
{
$data['cities'] = City::where("state_id",$request➡state_id)➡get(["city_id","city_name"]);
return response()➡json($data);
}
}
#Run below command for Test Laravel Dependent AJAX Dropdown
php artisan serve
#access in Browser
http://127.0.0.1:8000/laravel-dropdown
Note: Flow the Process shown in video.
😉Subscribe and like for more videos:
/ @chiragstutorial
💛Don't forget to, 💘Follow, 💝Like, 💖Share 💙&, Comment
Tutorial Link:
https://www.chirags.in/tutorials/lara...
Thanks & Regards,
Chitt Ranjan Mahto "Chirag"
_____________________________________________________________________
Note: All scripts used in this demo will be available in our website.
Link will be available in description.
#chirags
#chiragstutorial
#chiragslaraveltutorial
#chiragslaraveltutorials
#laraveltutorial
#laravel10
#laravelcourse
#installlaravel
#laravel_tutorial
#laravelphp