Nyerere
Nyerere

Reputation: 29

Displaying Json data on view ( LARAVEL )

I am working in laravel project and i want to display my json data from url into the view dashboard.blade, And this is the sample json data:

{"response":{"result":{"Contacts":{"row":[{"no":"1","fl":[{"val":"CONTACTID","content":"144120000000079041"},{"val":"First Name","content":"Tata"},{"val":"Last Name","content":"Nyerere"},{"val":"Email","content":"[email protected]"},{"val":"Account Name","content":"null"},{"val":"Phone","content":"255652400670"},{"val":"Mobile","content":"null"}]},{"no":"2","fl":[{"val":"CONTACTID","content":"144120000000069001"},{"val":"First Name","content":"Daniel"},{"val":"Last Name","content":"Nyerere"},{"val":"Email","content":"[email protected]"},{"val":"Account Name","content":"null"},{"val":"Phone","content":"255754897505"},{"val":"Mobile","content":"null"}]},{"no":"3","fl":[{"val":"CONTACTID","content":"144120000000065068"},{"val":"First Name","content":"Lawrence"},{"val":"Last Name","content":"Zoho"},{"val":"Email","content":"[email protected]"},{"val":"ACCOUNTID","content":"144120000000065066"},{"val":"Account Name","content":"Zoho"},{"val":"Phone","content":"1 888 900 9646"},{"val":"Mobile","content":"null"}]}]}},"uri":"/api/json/contacts/getrecords"}}

And here is my Controller method:

 public function getDashboard()
{
    $url      = "MY URL IS HERE";
    $json     = file_get_contents($url);
    $json     = json_decode($json, true);
    $response = $json['response'];
    $result   = $response['result'];
    $contact  = $result['Contacts'];
    $data     = $contact['row'];
    return view('dashboard')->with('posts', $data);
}

And here is my dashboard.blade that displays the json data on my view page:

       @foreach($posts as $post)
            @foreach($post['fl'] as $row)
                <article class="post">
                    @if($row['val'] == 'First Name')
                       First Name: {{ $row['content'] }}
                    @endif
                </article>
            @endforeach
        @endforeach

Can anyone assist me on the issue and how to write the foreach in dashboard.blade file as i want to display the data on my view as:

First Name    Last Name     Phone:
Tata          Nyerere       255754897505

As for now it only display

First Name: Tata

Please assist me as i have spent time to solve this I will appreciate your help guys. Thanks in advance`

Upvotes: 0

Views: 2075

Answers (3)

Toni Lam
Toni Lam

Reputation: 1

You may try json_decode(json_encode($data)).

If the data came from JsonResource, fields that are not existed in the original model will be omitted. It looks weird when you json_encode $data, it works, but not if you want to do something like $data->first_name.

Upvotes: 0

cjmling
cjmling

Reputation: 7278

Make a change from this line

$data = $contact['row'];

to

$data = $contact;

While the second error you have with index First Name and other is because your data isn't in those format to be accessible using like that.

Your first foreach will get into those no and fl index. You better reformat your response data before passing into blade.

enter image description here

Upvotes: 0

Tosho Trajanov
Tosho Trajanov

Reputation: 790

Use return view('dashboard')->with('leads', $contact); if you want to iterate $leads['row'] in the view.

Otherwise, you can just use return view('dashboard')->with('leads', $data); and iterate $leads in the view this way @foreach($leads as $rows)

Upvotes: 0

Related Questions