Support the ongoing development of Laravel.io →
Input Database Views
Last updated 2 years ago.
0

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>
Last updated 2 years ago.
0

Why don't you use Select2 , it has ajax support and I use it in my projects as well.

0

What is Select2?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.