我正在尝试在 laravel Blade 视图中使用嵌套对象中的数据。
我正在使用Laravel 4.2。
下面是数据示例"$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
)
)
)
我将其传递给视图"对象":
// 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);
在这里,我尝试在视图中检索一个值:
<h2>
{{ $content->h1-title->h3-sub-title }}
</h2>
我从上面得到的错误是:
未定义的属性:标准类::$h 1
我也尝试过:
<h2>
{{ $content->h1-title->h3-sub-title }}
</h2>
然后我收到以下错误:
语法错误、意外的"->"(T_OBJECT_OPERATOR)、预期出现","或 ';'
我认为我想要做的事情很明显:我希望能够根据需要访问嵌套对象的值。 我已经在谷歌上搜索了上面尝试的不同方法,现在我不知道 Laravel 是否期待不同的东西/不支持这个,或者我是否在做一些愚蠢的事情。
有关如何执行此操作的任何指导将不胜感激。
试试这个:
{{ $content->{'h1-title'}->{'h3-sub-title'} }}
您收到错误,因为您不能使用"-"调用属性。尽量避免使用带"-"的名称。更好的名字将是"h1_title"h3_sub_title"