shamelesshacker
shamelesshacker

Reputation: 185

How to use data in a nested object in a laravel Blade view?

I am trying to use data in a nested object in a laravel Blade view.

I am using Laravel 4.2.

Here is a sample of the data, "$arContent":

stdClass Object
(
    [h1-title] => stdClass Object
        (
            [h3-sub-title] => Safe, Affordable Self-Storage
            [img] => Array
                (
                    [0] => Caption 1
                    [1] => Caption 2
                    [2] => Caption 4
                )
        )
)

I pass it to the view, "objects":

// show the view and pass the page and domain recrods to it
return View::make('objects')
    ->with('page', $page)
    ->with('content', $arContent)
    ->with('domain', $domain);

And here I am trying to retrieve a value in the view:

<h2>
{{ $content->h1-title->h3-sub-title }}
</h2>

The error that I get from above is:

Undefined property: stdClass::$h1

I have also tried:

<h2>
    {{ $content->h1-title->h3-sub-title }}
</h2>

And I then get the following error:

syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';'

I think it's obvious what I am trying to do: I want to be able to access the values of the nested object as many "layers" down as I want. I have searched Google for the different approaches tried above and right now I don't know if Laravel is expecting something different / does not support this or if I am doing something stupid.

Any guidance on how to do this would be appreciated.

Upvotes: 1

Views: 1395

Answers (1)

xAoc
xAoc

Reputation: 3588

Try this:

{{ $content->{'h1-title'}->{'h3-sub-title'} }}

You get error, because you can't call propert with "-". Try to avoid names with "-". Much more better name will be "h1_title" "h3_sub_title"

Upvotes: 4

Related Questions