Dependent Dynamic dropdown using Angular Js and Laravel 5.6

Dependent Dynamic dropdown using Angular Js and Laravel 5.6

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 Table

In 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=latin1
Step1: 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);
    }
}
Step 4: Create dependent_dropdown.blade.php file

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 }}.

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort