Dependent country state city dropdown using jquery ajax in Laravel 5

Dependent country state city dropdown using jquery ajax in Laravel 5

Dependent country state city dropdown using jquery ajax in Laravel 5

In this tutorial, I am going to explain the dynamic dependent country state and city dropdown using jQuery Ajax in PHP Laravel and MySQL.

There are so many situation in the application where you will need to define dependent dropdown such as when you are working with e-commerce portal then there is need to define dependent dropdown for their category and sub-category because each category have many sub-categories so sub-category will be display on behalf of selected category.

In this post, i have three table :

  • countries table
  • states table
  • cities table

First i will display all country data in first select box and when i select any country from country dropdown then related state will be listed in second dropdown and same when i select state from state dropdown then related city will be listed in third dropdown.

Step 1: Create Tables

This is first step where i need to create table structure and for this i am going to create migration file for following table using Laravel php artisan command So open your terminal and run following line of code :

php artisan make:migration create_country_state_city_tables

Once command runs successfully then you will get a file in following path database/migrations and put following line of code in your migration file to create tables into your database.

  1. <?php
  2. use Illuminate\Database\Schema\Blueprint;
  3. use Illuminate\Database\Migrations\Migration;
  4. class CreateCountryStateCityTables extends Migration
  5. {
  6. public function up()
  7. {
  8. Schema::create('countries', function (Blueprint $table) {
  9. $table->increments('id');
  10. $table->string('sortname');
  11. $table->string('name');
  12. $table->timestamps();
  13. });
  14. Schema::create('states', function (Blueprint $table) {
  15. $table->increments('id');
  16. $table->string('name');
  17. $table->integer('country_id');
  18. $table->timestamps();
  19. });
  20. Schema::create('cities', function (Blueprint $table) {
  21. $table->increments('id');
  22. $table->string('name');
  23. $table->integer('state_id');
  24. $table->timestamps();
  25. });
  26. }
  27. public function down()
  28. {
  29. Schema::drop('countries');
  30. Schema::drop('states');
  31. Schema::drop('cities');
  32. }
  33. }

Now save this file and run migration using artisan command :

php artisan migrate

Now you will have three tables in your database that will use to get country state and their city on behalf of their relation.

countries table

states table

cities table

Ok, now we have countries, states and cities table.

Step 2: Define Route

In this step, i will define some route that handle url request.

  1. Route::get('api/dependent-dropdown','APIController@index');
  2. Route::get('api/get-state-list','APIController@getStateList');
  3. Route::get('api/get-city-list','APIController@getCityList');
Step 3: Create APIControlle.php Filer

As you see in above routes there are a APIController having index, getStateList, getCityList. So in this step i will create a APIController.php with following methods.

app/Http/Controllers/APIController.php
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests;
  4. use Illuminate\Http\Request;
  5. use DB;
  6. class APIController extends Controller
  7. {
  8. public function index()
  9. {
  10. $countries = DB::table("countries")->lists("name","id");
  11. return view('index',compact('countries'));
  12. }
  13. public function getStateList(Request $request)
  14. {
  15. $states = DB::table("states")
  16. ->where("country_id",$request->country_id)
  17. ->lists("name","id");
  18. return response()->json($states);
  19. }
  20. public function getCityList(Request $request)
  21. {
  22. $cities = DB::table("cities")
  23. ->where("state_id",$request->state_id)
  24. ->lists("name","id");
  25. return response()->json($cities);
  26. }
  27. }
Step 4: Create index.blade.php file

This is last step where i will create view index.blade.php file where all script will be written.

resources/view/index.blade.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Dependent country state city dropdown using ajax in PHP Laravel Framework</title>
  5. <link rel="stylesheet" href="http://www.expertphp.in/css/bootstrap.css">
  6. <script src="http://demo.expertphp.in/js/jquery.js"></script>
  7. </head>
  8. <body>
  9. <div class="container">
  10. <div class="panel panel-default">
  11. <div class="panel-heading">Dependent country state city dropdown using ajax in PHP Laravel Framework</div>
  12. <div class="panel-body">
  13. <div class="form-group">
  14. <label for="title">Select Country:</label>
  15. {!! Form::select('country', ['' => 'Select'] +$countries,'',array('class'=>'form-control','id'=>'country','style'=>'width:350px;'));!!}
  16. </div>
  17. <div class="form-group">
  18. <label for="title">Select State:</label>
  19. <select name="state" id="state" class="form-control" style="width:350px">
  20. </select>
  21. </div>
  22. <div class="form-group">
  23. <label for="title">Select City:</label>
  24. <select name="city" id="city" class="form-control" style="width:350px">
  25. </select>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. <script type="text/javascript">
  31. $('#country').change(function(){
  32. var countryID = $(this).val();
  33. if(countryID){
  34. $.ajax({
  35. type:"GET",
  36. url:"{{url('api/get-state-list')}}?country_id="+countryID,
  37. success:function(res){
  38. if(res){
  39. $("#state").empty();
  40. $("#state").append('<option>Select</option>');
  41. $.each(res,function(key,value){
  42. $("#state").append('<option value="'+key+'">'+value+'</option>');
  43. });
  44. }else{
  45. $("#state").empty();
  46. }
  47. }
  48. });
  49. }else{
  50. $("#state").empty();
  51. $("#city").empty();
  52. }
  53. });
  54. $('#state').on('change',function(){
  55. var stateID = $(this).val();
  56. if(stateID){
  57. $.ajax({
  58. type:"GET",
  59. url:"{{url('api/get-city-list')}}?state_id="+stateID,
  60. success:function(res){
  61. if(res){
  62. $("#city").empty();
  63. $.each(res,function(key,value){
  64. $("#city").append('<option value="'+key+'">'+value+'</option>');
  65. });
  66. }else{
  67. $("#city").empty();
  68. }
  69. }
  70. });
  71. }else{
  72. $("#city").empty();
  73. }
  74. });
  75. </script>
  76. </body>
  77. </html>

If you will get error like `HTML` or `FORM` class not found then click here to get solution :

Class form or html not found in Laravel 5

Phone: (+91) 8800417876
Noida, 201301