Reputation: 17
I'm developing a website with Laravel where I've a JSON response from server like below:
[{"id":1,"user_id":"1","patient_name":"kk","age":"44","sex":"Male"},
{"id":2,"user_id":"1","patient_name":"noor","age":"7","sex":"Male"},
{"id":3,"user_id":"1","patient_name":"noor","age":"44","sex":"Male"}]
How can I iterate through this JSON object so that I can show the data in a table with patient_name, age, sex column in blade view file?
Upvotes: 1
Views: 1737
Reputation: 3621
First you will have to convert the JSON to an array in the controller file using the json_decode() method as $array_data = json_decode($array, true)
, then you can pass the data to your view from the controller as return view('page', ["array_data" => $array_data]);
.
$array_data = json_decode($array, true);
return view('page', ["array_data" => $array_data]);
Note that the page
must be the name of your view blade template file name minus the .blade.php
, i.e. if your template is called page.blade.php
you have to use just page
.
Finally, you have to parse the passed data in your view blade template file like this:
<table>
<tr>
<td>id</td>
<td>User id</td>
<td>Patient name</td>
<td>Age</td>
<td>Sex</td>
</tr>
@foreach($array_data as $key=>$value){
<tr>
<td>{{$value["id"]}}</td>
<td>{{$value["user_id"]}}</td>
<td>{{$value["patient_name"]}}</td>
<td>{{$value["age"]}}</td>
<td>{{$value["sex"]}}</td>
</tr>
@endforeach
Upvotes: 1