1

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.

1
  • what about var_dump($content) in the view - it's not the sollution but maybe will help Commented Jan 14, 2015 at 15:14

1 Answer 1

4

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"

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

2 Comments

Wow. Awesome. Where in the world is that in the Blade docs?
@shamelesshacker - This has nothing to do with blade. It's straight up PHP.

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.