There are many errors in the code. You just copy and paste forgot to change these values.
For this, I use a separate controller and javascript.
In my kind
<div class="form-group"> <label align="right" for="name" class="control-label col-xs-2">Course :</label> <select class="col-md-5 input-sm" name="name" id="name"> @foreach ($menuItem as $menu) <option value="{{ $menu->id }}" placeholder="choose menu">{{ $menu->name }}</option> @endforeach </select> </div>
To get submenu items I use Javascript
<script type="text/javascript"> $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $(document).ready(function(){ $('#name').on('change',function(e){ console.log(e); var parentID= e.target.value; $.getJSON('/your current url/sub?parentID=' + parentID, function(data){ console.log(data); $('#child_id').empty(); $.each(data,function(index, child){ $('#child_id').append('<option value="'+child.id+'">'+child.name+'</option>'); }); }); }); });
in my route.php
I have
Route::get('your current url/sub',' MyJSController@loadchild ');
In my function MyJSCcontroller -> loadchild there is
public function loadchild() { $parent = Input::get('parentID'); $sub_menu= DB::table('menus') ->where('parent_id','=',$parent) ->select('id','name') ->get(); return response()->json($sub_menu); }
Your submenu that appears in your VIEW should look like this:
<div class="form-group"> <label align="right" for="child_id" class="control-label col-xs-2">Sub Menu :</label> <select class="col-md-5 input-sm" name="child_id" id="child_id" > </select> </div> public function create() { $menuItem = DB::table('menus') ->where('parent_id','=',0) ->get(); return view('menu.createmenu', compact('menuItem')); }
Make sure you import use Illuminate\Support\Facades\Input; use DB;
use Illuminate\Support\Facades\Input; use DB;
in your controller.
One more thing
Do not use {!! Form::close() !!}
{!! Form::close() !!}
due to security vulnerabilities. Use {{ Form::close() }}