The error is in the route, I should create parameter route like:
Route::get('dropdowns/stations/{id}', 'DropDownController@getStations');
And modify a bit the jquery script and DropDownController:
public function getStations($id)
{
$stations = Station::where('basin_id', '=', $id)->get();
$options = array();
foreach ($stations as $station) {
$options += array($station->id => $station->name);
}
return Response::json($options);
}
AJAX call:
<script type="text/javascript">
$(document).ready(function() {
$("#basin_id").change(function() {
$.getJSON("../dropdowns/stations/" + $("#basin_id").val(), function(data) {
var $stations = $("#station_id");
$stations.empty();
$.each(data, function(index, value) {
$stations.append('<option value="' + index +'">' + value + '</option>');
});
$("#station_id").trigger("change"); /* trigger next drop down list not in the example */
});
});
});
</script>
Why don't you use Select2 , it has ajax support and I use it in my projects as well.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community