Dynamic Dependent drop-down list in laravel

Dynamic Dependent drop-down list in laravel

Sometimes We Need a dependent drop-down value for filtering the data or sub categories according to category id in our project. so in this article, we will learn how to get a dependent drop down data from my SQL database.

In this article, we will find subcategories according to category id

We have two tables one is categories and the second is sub_categories.


Dynamic Dependent drop-down list in laravel


Dynamic Dependent drop-down list in laravel

Here we have to note that the parent table’s name should be used with under score id (_id) in sub_categories table’s column .

In my case, I used the categories table in the sub_categories table by categories_id.

Now go to your xyxController.php(app/Http/Controller), in my case, I will make changes in ArticleController.php


public function getCategories()
        $categories = DB::table('categories')->pluck("name","id");
        return view('article.create',compact('categories'));

    public function getSubCategories($id)
        $subcategories = DB::table("sub_categories")->where("category_id",$id)->pluck("name","id");
        return json_encode($subcategories);

Now go to web.php routes and use

Route::get('add-new-article', [ArticleController::class , 'getCategories']);
Route::get('dropdownlist/getsubcategories/{id}',[ArticleController::class , 'getSubCategories']);

Now go to the resources folder and create your blade file.


<form action="{{ URL::to('post-article-form') }}" enctype="multipart/form-data" method="post">@csrf

Laravel Dependent Dropdown
<div class="form-group"><label for="category"> Select category </label>
<select class="form-control" name="category" required="">
<option value="">Select</option>@foreach ($categories as $key =&gt; $value)
<option value="{{ $key }}">{{ $value }}</option>@endforeach
<div class="form-group"><label for="subCategory">Select SubCategory:</label>
<select class="form-control" style="width: 250px;" name="subCategory">
<button class="btn btn-primary" type="submit">Submit</button>

    Now add JQuery AJAX to get the route of sub_categories according to categoryId.

Here we will pass the ‘dropdownlist/getsubcategories/’ +categoryID’ in url of jQuery.ajax.
The below code will be executed after selecting category. Now According to the selected category our AJAX will filter the sub_categories with the help of the selected category id.

<script type="text/javascript">
            jQuery(document).ready(function ()
                       var categoryID = jQuery(this).val();
                             url : 'dropdownlist/getsubcategories/' +categoryID,
                             type : "GET",
                             dataType : "json",
                                jQuery.each(data, function(key,value){
                                   $('select[name="subCategory"]').append('<option value="'+ key +'">'+ value +'</option>');

Now save the project and run the following command in the terminal.

php artisan serve

and open the http://localhost:8000/add-new-article
After Successful running the server, you will get a result like this,
laravel dependent drop down

Dynamic Dependent drop-down list in laravel

Content Protection by DMCA.com
Spread the love

Leave a Comment