It's easy. First, you have some issues in your code I will share the new code with you.
<label for="country">Choose country</label>
<select name="country" id="country">
@foreach($countries as $id => $name)
<option value="{{$id}}">{{$name}}</option>
@endforeach
</select>
<label for="country">Choose country</label>
<select name="state" id="state">
@foreach ($states as $state)
<option value="{{$state['country_id']}}">{{$state['name']}}</option>
@endforeach
</select>
the next code will be in your controller and compact the variables only.
$states = [
[
'country_id' => 0, //index of country
'state_id' => 1,
'name' => 'NSW'
],
[
'country_id' => 0,
'state_id' => 2,
'name' => 'MQD'
],
[
'country_id' => 0,
'state_id' => 3,
'name' => 'PSA'
],
[
'country_id' => 0,
'state_id' => 4,
'name' => 'NOC'
],
[
'country_id' => 0,
'state_id' => 5,
'name' => 'WWA'
],
[
'country_id' => 1,
'state_id' => 6,
'name' => 'SSL'
],
[
'country_id' => 1,
'state_id' => 7,
'name' => 'PSQ'
],
];
$countries = [
'Canada',
'UK',
'USA'
];
return view('your_view_name', compact('states', 'countries'));
And finally the JS code you need to make change dynamically.
<script>
$(function () {
$('#country').on('change', function () {
let val = this.value;
console.log(val)
$('#state option').hide().filter(function () {
return this.value.indexOf(val) === 0;
})
.show();
})
.change();
});
</script>