Laravel: How do I parse this json data in view blade?

David Alrdrin picture David Alrdrin · May 6, 2015 · Viewed 110.6k times · Source

Currently this is my view

{{ $leads }}

And this is the output

{"error":false,"member":[{"id":"1","firstName":"first","lastName":"last","phoneNumber":"0987654321","email":"[email protected]","owner":{
"id":"10","firstName":"first","lastName":"last"}}]}

I wanted to display something like this

Member ID: 1
Firstname: First
Lastname: Last
Phone: 0987654321

Owner ID: 10
Firstname: First 
Lastname: Last

Answer

Maxim Lanin picture Maxim Lanin · May 6, 2015

It's pretty easy. First of all send to the view decoded variable (see Laravel Views):

view('your-view')->with('leads', json_decode($leads, true));

Then just use common blade constructions (see Laravel Templating):

@foreach($leads['member'] as $member)
    Member ID: {{ $member['id'] }}
    Firstname: {{ $member['firstName'] }}
    Lastname: {{ $member['lastName'] }}
    Phone: {{ $member['phoneNumber'] }}

    Owner ID: {{ $member['owner']['id'] }}
    Firstname: {{ $member['owner']['firstName'] }} 
    Lastname: {{ $member['owner']['lastName'] }}
@endforeach