1

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?

2
  • I see you are new to Stack overflow, would you also consider upvoting the correct answer(s) so other people can clearly see that it could be a possible solution? This could help other people a lot. Commented Oct 12, 2018 at 7:32
  • Well I need 15 reputation points for upvoting but I've only 7. I'll upvote when I'll get the points @SvenHakvoort Commented Oct 13, 2018 at 7:30

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.