The Dynamic Dependent Select Box is mostly used for the Nation-State-City dropdown in various web applications. In this article, we are going to build up this dropdown feature using Laravel, Angular JS and MySQL. In this dropdown, State is connected with the Nation and City is connected with the State.
As soon as you will select the nation, state table will be reloaded with respective states of that nation and after selecting the states, city table will be reloaded with respective cities of that state.
The data will be brought from the database without reloading the page using Laravel, Angular JS and MySQL.
Go ahead and try this, you will see that its quite handy and easy to build up.
Step1: Create TableIn this first step, you can create tables using migration file or directly run the below code to create tables in your database.
You will know how to create tables using migration file by clicking :Dependent dropdown in Laravel
CREATE TABLE `countries` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
CREATE TABLE `states` ( `id` int(11) NOT NULL AUTO_INCREMENT, `country_id` int(11) NOT NULL, `name` varchar(100) NOT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
CREATE TABLE `cities` ( `id` int(11) NOT NULL AUTO_INCREMENT, `state_id` int(11) NOT NULL, `name` varchar(100) NOT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1Step1: Add Route
Route::get('dependent-dropdown','APIController@index'); Route::get('get-country-list','APIController@getCountryList'); Route::get('get-state-list','APIController@getStateList'); Route::get('get-city-list','APIController@getCityList');Step 3: Create APIControlle.php File
<?php namespace App\Http\Controllers; use App\Http\Requests; use Illuminate\Http\Request; use DB; class APIController extends Controller { public function index() { return view('dependent_dropdown'); } public function getCountryList(Request $request) { $countries = DB::table("countries") ->select("name","id") ->get(); return response()->json($countries); } public function getStateList(Request $request) { $states = DB::table("states") ->where("country_id",$request->country_id) ->select("name","id") ->get(); return response()->json($states); } public function getCityList(Request $request) { $cities = DB::table("cities") ->where("state_id",$request->state_id) ->select("name","id") ->get(); return response()->json($cities); } }
In this last step, I will create "dependent_dropdown.blade.php" file.
<!DOCTYPE html> <html> <head> <title>Dependent Dynamic dropdown using Angular Js and Laravel 5.6 - ExpertPHP.in</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> </head> <body> <div class="container" style="width:500px;"> <h3 align="center">Dependent Dynamic dropdown using Angular Js and Laravel 5.6 - ExpertPHP.in</h3> <br /> <div ng-app="demoapp" ng-controller="democontroller" ng-init="loadCountry()"> <select name="country" ng-model="country" class="form-control" ng-change="loadState()"> <option value="">Select country</option> <option ng-repeat="country in countries" value="<% country.id %>"><% country.name %></option> </select> <br /> <select name="state" ng-model="state" class="form-control" ng-change="loadCity()"> <option value="">Select state</option> <option ng-repeat="state in states" value="<% state.id %>"><% state.name %></option> </select> <br /> <select name="city" ng-model="city" class="form-control"> <option value="">Select city</option> <option ng-repeat="city in cities" value="<% city.id %>"> <% city.name %> </option> </select> </div> </div> </body> </html> <script> var app = angular.module('demoapp', [], function($interpolateProvider) { $interpolateProvider.startSymbol('<%'); $interpolateProvider.endSymbol('%>'); }); app.controller("democontroller", function($scope, $http){ $scope.loadCountry = function(){ var url="{{url('get-country-list')}}"; $http.get(url) .success(function(data){ $scope.countries = data; }) } $scope.loadState = function(){ var url="{{url('get-state-list')}}?country_id="+$scope.country; $http.get(url) .success(function(data){ $scope.states = data; }) } $scope.loadCity = function(){ var url="{{url('get-city-list')}}?state_id="+$scope.state $http.get(url) .success(function(data){ $scope.cities = data; }); } }); </script>
As we know, both Angular and Laravel have same syntax to use double curly brackets :
<code>{{ variable }}</code>
So I changed the Angular Tags by using following code :
var app = angular.module('demoapp', [], function($interpolateProvider) { $interpolateProvider.startSymbol('<%'); $interpolateProvider.endSymbol('%>'); });
Now Angular will use <% variable %>
and Laravel will use {{ variable }}
.