【发布时间】:2020-03-17 19:07:27
【问题描述】:
我正在尝试移动在框架之外创建的 webapp CRUD 解决方案, 现在将它移到 Laravel 中。下面你会看到“标准 PHP”部分,这是一个可以工作的代码,以及“Laravel 不能工作。
问题:如何在 Laravel 中编写嵌套的 foreach 循环,与在标准 PHP 示例中编写的一样?
Laravel
<table>
<!-- https://laravel.com/docs/5.7/blade#loops -->
@foreach ($response_body as $key => $value)
@foreach ($value as $level2data => $array)
<tr>
<td>
Hello
</td>
</tr>
@endforeach
@endforeach
</table>
laravel 代码结果(浏览器打印):
@foreach ($response_body as => $value)
@foreach ($value as $level2data => array)
@endforeach
@endforeach
Hello
标准 PHP
<table>
<?php foreach ($response_body as $key => $value):?>
<?php foreach ($value as $level2data => $array):?>
<tr>
<td id="email">
<?php echo "hello" . "\r\n"; ?>
</td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
【问题讨论】:
-
你的代码中有一些明显的错误
-
1.您在循环外启动
<table>,但在循环内关闭表</table> -
这一行:
@foreach ($resonse_body as => $value)我想你的意思是@foreach ($resonse_body as $value) -
因此,如果您修复了小 TYPO,您至少会更接近解决方案
-
@RiggsFolly 谢谢,我更新了代码,调整了错别字。我有点困惑为什么“foreach”和“endforeach”会打印到浏览器中。
标签: php laravel loops foreach nested