2

I'm pulling data out of a blade template using:

@foreach ($links as $link)
    <p>{{ ($link['title']) }} </p>
@endforeach

I'd like to use:

@foreach ($links as $link)
    <p>{{ ($link->title) }} </p>
@endforeach

But I'm not storing my data as an object in the controller. How do I store data as an object?

My controller code:

foreach($json['items'] as $item) {


    $link = [

        'vidId' => $item['id']['videoId'],
        'title' => $item['snippet']['title'],
        'thumb' => $item['snippet']['thumbnails']['high']['url']
    ];

    $links[] = $link;

}

$data['links'] = $links;
$data['v'] = $v;

return view('results', $data);

2 Answers 2

2

Just cast the array to object:

$obj_links = (object)$links;
Sign up to request clarification or add additional context in comments.

Comments

1

I rewrote the controller like so:

foreach($json['items'] as $item) {

    $link = new \stdClass;
    $link -> vidId = $item['id']['videoId'];
    $link -> title = $item['snippet']['title'];
    $link -> thumb = $item['snippet']['thumbnails']['high']['url'];

    $links[] = $link;

}

$data = [
    'links' => $links,
    'v' => $v
];


return view('results')->with($data);

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.