Display Countries and state drop down where the state dependent on the country
HI. So i want to do dropdown of countries and state.So far I can display my country using the 'pluck'. Now I dont know how to make the state depent on the country choosen to show in the drop down .
You can do that using jquery and AJAX. You have to create a separate method that returns all states based on their respective country ids.
<?php
public function states($country_id){
//Assuming you've created States model for `States table
$states = States::where('country_id', $country_id)->select('id','name')->get();
return response()->json($states);
}
?>
This will returns all states for given country id. You have to use this URL in AJAX.
//Id of yoyr country dropdown
$("#country").on('change', function(){
var selectedCountry = $("#country option:selected").val();
$.ajax({
url: "<route_for_above_function>",
success: function(response){
var html = '';
$("#country").html(''); //For clearing old option list
for(var i = 0; i < response.length; i++) {
var obj = response[i];
html += '<option value="'+obj.id+'">'+obj.name+'</option>';
}
$("#country").html(html);
}
});
});
What do you mean by ** You're just using package**? Are you have a JSON file which you are using as a data source? How your package is maintaining data for your country and state? I would suggest storing data in tables and use Laravel Models, You can then use Eloquent relationships, Route model bindings.