I'm currently working on an edit view http://127.0.0.1:8000/dgj/editar_ejuridica/7
, I have two dependent select 'has (' city ')? 'has-error': ''}} ">
*City:
@foreach($ciudades as $ciudad => $value)
<option value="{{$value->id}}">{{$value->city_cty}}</option>
@endforeach
</select>
</div>
</div>
<div class="form-group {{ $errors->has('municipio') ? 'has-error' : '' }}">
<label class="col-sm-3 control-label">
*Municipio:
</label>
<div class="col-sm-7">
<select class="form-control dynamic" id="municipio" name="municipio">
<option value="0" disabled="true" selected="true">Municipios</option>
</select>
</div>
</div>'
I use this AJAX code:
<script type="text/javascript">
$('#ciudad').on('change', function(e){
console.log(e);
var ciudad_id = e.target.value;
$.get('select_ciudad?ciudad_id=' + ciudad_id,function(data) {
$('#municipio').empty();
$('#municipio').append('<option value="0" disable="true" selected="true"></option>');
$.each(data, function(fetch, regenciesObj){
$('#municipio').append('<option value="'+ regenciesObj.id +'">'+ regenciesObj.municipality_mty +'</option>');
})
});
});
the route for this AJAX is the following:
Route::get('select_ciudad', 'SelectController@fetch')->name('dgj.vwempresas.fetch');
and this is my controller:
class SelectController extends Controller
{
function fetch(Request $request)
{
$ciudad_id = Input::get('ciudad_id');
$regencies = Municipio::where('cityid_mty', '=', $ciudad_id)->get();
return response()->json($regencies);
}
}
my select are City and municipality, in the view create I have the same with the same script and the same controller and it works correctly, in the edit view I am working does not work