I have a json file like this below:
[
{
"Region": "Naypyitaw Union Territory",
"Town ": "Za Bu Thi Ri Township",
"Quarter ": "Zay Ya Theik Di Quarter",
"Postal Code": 1501001
},
{
"Region": "Naypyitaw Union Territory",
"Town ": "Za Bu Thi Ri Township",
"Quarter ": "Pyin Nyar Theik Di Quarter",
"Postal Code": 1501002
}]
In my controller, I passed the file like this below:
public function create()
{
$jsonString = file_get_contents(base_path('resources/data/address.json'));
$details = json_decode($jsonString, true);
return view('users.create')->with('details', $details);
}
In my create.blade.php file I want to show the dropdown menus as Region, Town, and Quarter and Postal Code based on the user choice. For eg. If the user choose Region as "Naypyitaw Union Territory", then the user should able to Town associated in that region. But I do not know how to loop through it.
<select name="region" id="">
foreach
<option value="">{{$details->Region}}</option>
endforeach
</select>
CodePudding user response:
Simply access the decoded JSON as an array and iterate over it using blade tags:
<select name="region" id="">
@foreach($details as $detail)
<option value="">{{$detail['Region']}}</option>
@endforeach
</select>