Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

myworkaccount5000's avatar

Pass HTTP Client JSON response to View (completely lost here)

Hello!

I'm only just learning Laravel and I'm trying to use it in the simplest way: get data (JSON) from an HTTP endpoint and loop through it in a Blade template.

{
    "records": [
        {
            "id": "recCC9bmU",
            "createdTime": "2022-11-30T20:51:57.000Z",
            "fields": {
                "Revision Number": "P1",
                "Project Number": "0190",
                "Cut Ticket BOM": [
                    "recVH4co7U",
                    "recR0AogvB"
                ],
                "Color Code": "OO",
                "Description": "description",
                "Size": "LG"
            }
        },
        {
            "id": "recPzRfs7",
            "createdTime": "2022-12-05T18:06:18.000Z",
            "fields": {
                "Project Number": "0190",
                "Revision Number": "P1",
                "Qty.": 32
            }
        }
    ]
}

I want to loop over records in my Blade template.

I've looked at the documentation and I've read through several search results. I've tried at least 30 variations of the code below but nothing is working and so now I'm thoroughly lost. I thought I had found "the answer" several times but always end up with an error of some kind:

Route::get('/cut-ticket', function () {
    // forum system is forcing me to obscure the URL; rest assured it works
    $cutTicketUrl = '...url...';

    $response = Http::withToken('keypb')->acceptJson()->get($cutTicketUrl);
    $data = $response->json();

    return view('cut-ticket', ['data' => $data->records]);
});

This results in Attempt to read property "records" on array.

I don't understand what that error message is trying to say. $data itself is not an array, it's an object. records is the array. Why is records not available from $data?

0 likes
10 replies
myworkaccount5000's avatar

@jlrdw Thanks for your response but that page you linked to is regarding JavaScript. I may have missed it but I didn't find anything related to my issue.

I'll do some more searching here on the forum but if you have some info to share I would really appreciate it.

jlrdw's avatar

@myworkaccount5000 if you're not using Ajax or JavaScript just json_decode and loop the array.

Which by the way not only I but many others have given answers on here.

That post was demonstrating how you get data from the json response.

myworkaccount5000's avatar
Level 1

@jlrdw I ended up finding a post on Stackoverflow that lead me to the answer indirectly. It turns out my major problem was the wrong syntax. I have been writing $data->records when I should have doing $data['records'].

One thing you have to keep in my when helping people is that things which are obvious to you are not always obvious to someone else. It can be hard to see from the "newbie's" perspective.

edit: Forgot to mention that I am not using JavaScript. My view currently looks like:

        @foreach ($data as $datum)
            {{ $datum['id'] }}<br/>
        @endforeach
jlrdw's avatar

@myworkaccount5000 yeah, your post sounded like you wanted to loop (iterate) json whereas you are looping an array, so I'm glad you solved it.

myworkaccount5000's avatar

@jlrdw JSON is JavaScript Object Notation and is the format returned by the endpoint. This is the way data is normally transmitted with HTTP REST APIs. I wouldn't strictly say this is an array because it has named key-pairs and objects. I'm more familiar with the JavaScript world and JSON "just works" over there for the most part.

I still contend that I do want to iterate a JSON object. I believe it would have been misleading to ask how to iterate over an array.

myworkaccount5000's avatar

@jlrdw Are we talking about different things? I'm just not sure. I'm not doing ajax (an old term for asynchronous JavaScript). Where did that come from?

I'm trying to use a JSON string in PHP/Laravel but it's not working. Your gist also doesn't seem terribly relevant because it's straight PHP and not utilizing Laravel. Again, this is not obvious to me what you're trying to share.

I'm now unable to access an object in an element.

Is there any way to turn a JSON object into something Laravel fully understands without jumping through hoops?

jlrdw's avatar

@myworkaccount5000 laravel is php, so I apologize, I do not understand where it is you are having trouble.

If you are referring to Blade it would be very similar to the same way you loop in PHP templating.

But there are many past examples of that posted here.

And I thought the stack overflow answer solved the problem.

myworkaccount5000's avatar

@jlrdw First of all, thanks for persisting with me. I appreciate it. As I was writing my response to try to clarify my latest issue, I found my mistake: a typo. ugh

Please or to participate in this conversation.