当我循环数据库的结果时,我正在构建一个导航菜单,它与下面的代码一起正常工作
foreach ($menu as $item) {
echo $item->name_english . ' ';
if ($item->childs->count()) {
foreach ($item->childs as $row) {
echo $row->name_english . ' | ';
}
}
}这证明了输出
第一、二、四、三、四、四、三、四、四、三、三、四、三、二、二、三、四、四、三、二、四、三、四、三、四、四
当我在小枝中使用相同的代码时,childs循环没有结果。
{% if menu %}
<ul>
{% for item in menu %}
<li>{{ item.name_english }}</li>
{% if item.childs.count() %}
<ul>
{% for stuff in item.childs %}
<li>{{ stuff.name_english }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
</ul>
{% endif %}这是输出
发布于 2015-10-16 08:47:29
若要检查细枝中数组的计数,请使用length过滤器。此外,当你使用树枝时,你应该更加具体。使用menu is not null而不是menu。
{% if menu is not null and menu|length > 0 %}
<ul>
{% for item in menu %}
<li>{{ item.name_english }}</li>
{% if item.childs|length > 0 %}
<ul>
{% for stuff in item.childs %}
<li>{{ stuff.name_english }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
</ul>
{% endif %}https://stackoverflow.com/questions/33158529
复制相似问题