Laravel Dependent Dropdown Example Tutorial From Scratch

Today, We want to share with you Laravel Dependent Dropdown Example Tutorial From Scratch.In this post we will show you Create Dependent Drop down in Laravel, hear for Laravel Dependent Dropdown Tutorial With Example we will give you demo and example for implement.In this post, we will learn about Dependent Select Box for Laravel Beginners with an example.

Laravel Dependent Dropdown Example Tutorial From Scratch

There are the Following The simple About Laravel Dependent Dropdown Example Tutorial From Scratch Full Information With Example and source code.

As I will cover this Post with live Working example to develop Dependant Deop down in Laravel, so the Ajax Dynamic Dependent Dropdown in Laravel for this example is following below.

  • student.php
  • subjects.php

Step 1: Create Model

create a migration for demo_student as well as demo_subjects tables using Laravel simple PHP artisan command run and migrated.

Also Read :  Retrieve Multiple Checkbox Selected Options using VueJS

php artisan make:model Model\Student 
php artisan make:model Model\Subject

App/Http/Model/Student.php

<?php 
namespace App\Model; 
use Illuminate\Database\Eloquent\Model; 
class Student extends Model 
{ 
  // here all the filed required
}

App/Http/Model/Subject.php

<?php 
namespace App\Model\Demo\student_subject_select; 
use Illuminate\Database\Eloquent\Model; 
class Subject extends Model 
{ 
  // here all the filed required
}

Step 2: Create Controller

make a Laravel Controller StudentSubjectController

StudentSubjectController.php

<?php
namespace App\Http\Controllers\Demo\student_subject_select;
use App\Http\Controllers\Controller;
use App\Model\Demo\student_subject_select\Subject;
use App\Model\Demo\student_subject_select\Student;
use Illuminate\Http\Request;
class StudentSubjectController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $students = Student::all();
        return view('demo.student_subject_select.index',compact('students'));
    }
    public function destroy($id)
    {
        //
    }
    public function findSubjectWithStudentID($id)
    {
        $subject = Subject::where('student_id',$id)->get();
        return response()->json($subject);
    }
}

Step 3: Laravel Define Route

make the Laravel route for the simple function findSubjectWithStudentID

// this Laravel main route can return the student with the student id
Route::get('findSubjectWithStudentID/{id}','Demo\student_subject_select\[email protected]');

Step 4: Make a blade file View

And then We are gonna to make the Laravel balde file view for that Dependent Drop down in Laravel for the subject respect to the student inside the Laravel folder Like as resource/view/dropdown.

Also Read :  Simple Angular 6 Observables Example

//resource/view/dropdown
@extends('master.app')
@section('main-content')
<div class="col-md-8 col-md-offset-2">
  <div class="panel panel-success">
      <div class="panel-heading">
       Ajax Dynamic Dependent Dropdown in Laravel Example
      </div>
      <div class="panel-body">
    <form action="{{ route('dynamic-dropdown-laravel.store') }}" method="post">
     {{ csrf_field() }}
     <div class="row">
      <div class="col-md-6">
       <div class="form-group {{ ($errors->has('roll'))?'has-error':'' }}">
        <label for="roll">Student <span class="required">*</span></label>
        <select name="student" class="form-control" id="student">
         <option value="">-- Select Student --</option>
         @foreach ($students as $student)
          <option value="{{ $student->id }}">{{ ucfirst($student->student_name) }}</option>
         @endforeach
        </select>
     </div>
      </div>
      <div class="col-md-6">
       <div class="form-group {{ ($errors->has('name'))?'has-error':'' }}">
        <label for="roll">Subject </label>
        <select name="subject" class="form-control" id="subject">
        </select>
     </div>
      </div>
     </div>
   </form> 
      </div>
    </div>
</div>
    
@endsection

Step 5: Make Laravel jQuery Ajax Call

@section('script')
<!-- simple jQuery first, and then Popper.js, After Bootstrap JS -->
<script type="text/javascript" src="{{ asset('assets/js/jquery-3.3.1.min.js') }}"></script>
    <script type="text/javascript" src="{{ asset('assets/js/bootstrap.min.js') }}"></script>
 <script>
         $(document).ready(function() {
        $('#student').on('change', function() {
            var studentID = $(this).val();
            if(studentID) {
                $.ajax({
                    url: '/findSubjectWithStudentID/'+studentID,
                    type: "GET",
                    data : {"_token":"{{ csrf_token() }}"},
                    dataType: "json",
                    success:function(data) {
                        //console.log(data);
                      if(data){
                        $('#subject').empty();
                        $('#subject').focus;
                        $('#subject').append('<option value="">-- Select Subject --</option>'); 
                        $.each(data, function(key, value){
                        $('select[name="subject"]').append('<option value="'+ key +'">' + value.subject_name+ '</option>');
                    });
                  }else{
                    $('#subject').empty();
                  }
                  }
                });
            }else{
              $('#subject').empty();
            }
        });
    });
    </script>

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read :  Laravel 5.6 Dependent Dynamic dropdown Example Tutorial

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Dependent Dropdown Example Tutorial From Scratch.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Recommended For You.

jQuery Check if Element Exists Example
Today, We want to share with you jQuery Check if Element Exists Example.In this post we will show you ,

Add a Comment

Your email address will not be published. Required fields are marked *