0

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`

3 Answers 3

0

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)

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried with return view('dashboard')->with('leads', $contact); with $leads['row'] as $rows i get the error 'Undefined index: First Name (View: C:\wamp\www\ZOHOCRMWIKI\resources\views\dashboard.blade.php) '
0

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

4 Comments

Still error Undefined index: First Name (View: C:\wamp\www\ZOHOCRMWIKI\resources\views\dashboard.blade.php)
@Nyerere look at the updated answer. You have to code more to get it working.
Can you assist on how can i reformat the json content above? @cjmling
Look now: my controller method:-
0

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.

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.