Tagged: laravel dependent dropdown

Ajax Dynamic Dependent Dropdown using Laravel Example 0

Ajax Dynamic Dependent Dropdown using Laravel Example

Today, We want to share with you Ajax Dynamic Dependent Dropdown using Laravel Example.In this post we will show you country state city drop down list using ajax in laravel, hear for Dependent Select Box for Laravel Beginners we will give you demo and example for implement.In this post, we will learn about laravel, jquery ajax categories and subcategories, select dropdown with an example.

Ajax Dynamic Dependent Dropdown using Laravel Example

There are the Following The simple About Ajax Dynamic Dependent Dropdown using Laravel Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop how to show selected value in dropdown in laravel 5.7, so the country state city drop down list in laravel 5.7 for this example is following below.

Step 1: Create Laravel Model

php artisan make:model Model\MyState 
php artisan make:model Model\MyCity

App/Http/Model/MyState.php

<?php 
namespace App\Model; 
use Illuminate\Database\Eloquent\Model; 
class MyState extends Model 
{ 
	// Your Logic
}

App/Http/Model/MyCity.php

<?php 
namespace App\Model\Demo\live_drop_down; 
use Illuminate\Database\Eloquent\Model; 
class MyCity extends Model 
{ 
	// Your Logic
}

Step 2: Create Laravel Controller

DependentDropdownController.php

    <?php
    namespace App\Http\Controllers\Demo\live_drop_down;
    use App\Http\Controllers\Controller;
    use App\Model\Demo\live_drop_down\MyCity;
    use App\Model\Demo\live_drop_down\MyState;
    use Illuminate\Http\Request;
    class DependentDropdownController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
            $name_states = MyState::all();
            return view('demo.live_drop_down.index',compact('name_states'));
        }
        public function destroy($id)
        {
            //
        }
        public function searchYourCity($id)
        {
            $name_city = MyCity::where('name_state_id',$id)->get();
            return response()->json($name_city);
        }
    }

Step 3: Define a Laravel Route

    Route::get('searchYourCity/{id}','Demo\live_drop_down\[email protected]');

Step 4: Make Laravel Blade View

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">
			<h3>How To Create Dependent Drop down in Laravel</h3>
          </div>
          <div class="pakainfo panel-body">
        <form action="{{ route('dynamic-dropdown-laravel.store') }}" method="post">
         {{ csrf_field() }}
         <div class="pakainfo row">
          <div class="pakainfo col-md-6">
           <div class="pakainfo form-group {{ ($errors->has('roll'))?'has-error':'' }}">
            <label for="roll">MyState <span class="required">*</span></label>
            <select name="name_state" class="pakainfo form-control" id="name_state">
             <option value="">-- Select MyState --</option>
             @foreach ($name_states as $name_state)
              <option value="{{ $name_state->id }}">{{ ucfirst($name_state->name_state_name) }}</option>
             @endforeach
            </select>
         </div>
          </div>
          <div class="col-md-6">
           <div class="form-group {{ ($errors->has('name'))?'has-error':'' }}">
            <label for="roll">MyCity </label>
            <select name="name_city" class="form-control" id="name_city">
            </select>
         </div>
          </div>
         </div>
       </form> 
	   <a href="https://pakainfo.com/" target="_blank" alt="pakainfo" title="pakainfo">Free Download Example - Pakainfo.com</a>
          </div>
        </div>
    </div>
        
    @endsection

Step 5: Create jQuery Ajax

    @section('script')
    <!-- jQuery first, then Popper.js, then 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() {
            $('#name_state').on('change', function() {
                var getStId = $(this).val();
                if(getStId) {
                    $.ajax({
                        url: '/searchYourCity/'+getStId,
                        type: "GET",
                        data : {"_token":"{{ csrf_token() }}"},
                        dataType: "json",
                        success:function(data) {
                            //console.log(data);
                          if(data){
                            $('#name_city').empty();
                            $('#name_city').focus;
                            $('#name_city').append('<option value="">-- Select MyCity --</option>'); 
                            $.each(data, function(key, value){
                            $('select[name="name_city"]').append('<option value="'+ key +'">' + value.name_city_name+ '</option>');
                        });
                      }else{
                        $('#name_city').empty();
                      }
                      }
                    });
                }else{
                  $('#name_city').empty();
                }
            });
        });
        </script>

Angular 6 CRUD Operations Application Tutorials

Related Search: Dependent Select Box for Laravel Beginners, Ajax Dynamic Dependent Dropdown in Laravel, Laravel Dependent Dropdown, dynamic dropdown in laravel, laravel ajax dropdown, laravel, jquery ajax categories and subcategories, select dropdown, select2 dependent dropdown laravel, dependent drop down list using jquery, country state city drop down list in laravel, country state city drop down list using ajax in laravel, how to show selected value in dropdown in laravel 5

Read :

Summary

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

I hope you get an idea about Ajax Dynamic Dependent Dropdown using Laravel Example.
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.

Laravel Dependent Dropdown Example Tutorial From Scratch 0

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.

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.

//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 :

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.