简体   繁体   中英

Blade foreach cast output to an object

I am trying to figure out how I can give my data output in the blade file, the Laravel look;

like $data->name

But I can't get the output to be casted as an object. I think I have to make an array of the data before I can loop it proper in a foreach but this doesn't feel like the right way.

I am relatively new to Laravel and I want to do this the nice way, can someone point me in the right direction? Thanks in advance

Controller:

$data = collect($this->api->organization->index())->toArray();

return View::make('pages.organization.index', array('data' => $data[0]));

View:

@foreach($data as ((object)$organization))
    {{ $organization->name }}
@endforeach

I know this will not work, but I think it illustrates my question a little bit.

EDIT

What I didn't realize is that $data = collect($this->api->organization->index()); is returning an array with all the data arrays inside because I didn't name it in my return like this:

return (object)['all' => $data];

After adding all I could reference the code inside my view like I wanted to. I know this is not a very detailed answer, if you run into the same problem message me I'll edit the answer.

Object:

$data = collect($this->api->organization->index());
@foreach($data as $organization))
    {{ $organization->name }}
@endforeach

Array:

$data = collect($this->api->organization->index())->toArray();
@foreach($data as $organization))
    {{ $organization['name'] }}
@endforeach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM