Laravel - Dynamic Dependant Select Box using JQuery Ajax Example - Part 2

By Hardik Savani November 5, 2023 Category : PHP Laravel Bootstrap jQuery MySql Ajax

After complete our fist part of Laravel 5 - Dynamic Dependant Select Box using JQuery Ajax Example, Now we have to do main task for Dynamic select box value. In this part we follow two step for create Controller File and Laravel Blade File.

In this part we write code for how to manage controller method and how to give response them. So let's follow this bellow remaining two step of change drop down options on change of parent dropdown. So let's follow:

Step 5: Create Controller

In this point, now we should create new controller as AjaxDemoController in this path app/Http/Controllers/AjaxDemoController.php. this controller will manage layout and ajax post request, So run bellow command for generate new controller:

php artisan make:controller AjaxDemoController

Ok, now put bellow content in controller file:



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class AjaxDemoController extends Controller



* Show the application myform.


* @return \Illuminate\Http\Response


public function myform()


$countries = DB::table('countries')->pluck("name","id")->all();

return view('myform',compact('countries'));



* Show the application selectAjax.


* @return \Illuminate\Http\Response


public function selectAjax(Request $request)



$states = DB::table('states')->where('id_country',$request->id_country)->pluck("name","id")->all();

$data = view('ajax-select',compact('states'))->render();

return response()->json(['options'=>$data]);




Step 6: Create View

In Last step, let's create two blade files as listed bellow:



One for render layout of form and another blade file for render options layout, so proceed to create both file one by one.


<!DOCTYPE html>



<title>Laravel 5 - onChange event using ajax dropdown list</title>

<link rel="stylesheet" type="text/css" href="">

<script src=""></script>



<div class="container">

<h1>Laravel 5 - Dynamic Dependant Select Box using JQuery Ajax Example</h1>

{!! Form::open() !!}

<div class="form-group">

<label>Select Country:</label>

{!! Form::select('id_country',[''=>'--- Select Country ---']+$countries,null,['class'=>'form-control']) !!}


<div class="form-group">

<label>Select State:</label>

{!! Form::select('id_state',[''=>'--- Select State ---'],null,['class'=>'form-control']) !!}


<div class="form-group">

<button class="btn btn-success" type="submit">Submit</button>


{!! Form::close() !!}


<script type="text/javascript">


var id_country = $(this).val();

var token = $("input[name='_token']").val();


url: "<?php echo route('select-ajax') ?>",

method: 'POST',

data: {id_country:id_country, _token:token},

success: function(data) {










<option>--- Select State ---</option>


@foreach($states as $key => $value)

<option value="{{ $key }}">{{ $value }}</option>



Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

Now you can open bellow URL on your browser:


I hope it can help you...