php - Laravel 5.3 foreach 来自 2 个不同模型的 2 个集合
问题描述
看法
@foreach ($duplicates as $duplicate)
<tr>
<td style="text-align: center;">{{ $duplicate->topic->id }}</td>
<td style="text-align: center;">{{ $duplicate->topic->title }}</td>
<td style="text-align: center;">{{ $duplicate->total }}</td>
@foreach($choices as $choice)
<td style="text-align: center;">{{ $choice->question_number }}</td>
<td><a class="btn btn-default" href="choices/{{ $choice->id }}/edit">Шинэчлэх</a></td>
@endforeach
</tr>
@endforeach
我想要这样的结果
@foreach ($duplicates as $duplicate)
<tr>
<td style="text-align: center;">{{ $duplicate->topic->id }}</td>
<td style="text-align: center;">{{ $duplicate->topic->title }}</td>
<td style="text-align: center;">{{ $duplicate->total }}</td>
<td style="text-align: center;">{{ $choice->question_number }}</td>
<td><a class="btn btn-default" href="choices/{{ $choice->id }}/edit">Шинэчлэх</a></td>
</tr>
@endforeach
我想要像 @foreach(duplicates as $duplicate, $choices as $choice) 这样的结果。但我知道我会出错。但如何?
解决方案
你可以试试
@foreach (array_combine($duplicates->toArray(), $choices->toArray()) as $duplicate => $choice){
// Do some stuff
@endforeach
如果 Laravel 的刀片语法不允许这样做,那么您总是可以在纯 PHP 中生成 html 并放弃刀片语法。这一切都假设 $duplicates 和 $choices 都是查询对象,从您的代码来看,它们似乎是。
推荐阅读
- c# - 在玩家范围内如何使敌人攻击?
- file - 是否可以从第一列包含与其他文件名称匹配的标识符的文件中提取某些行?
- javascript - 如何将 Fetch 调用的输出分配给变量 (JavaScript)
- typescript - 未捕获的语法错误:请求的模块“https://deno.land/std/uuid/mod.ts”未提供名为“v4”的导出
- excel - 在协作文档中分配特定列
- android - 我可以看到前台服务正在运行,但在“运行服务选项卡”中我只能看到一个进程正在运行
- javascript - 我可以通过 html 中的脚本标记属性将数组对象传递给 javascript 吗?
- apache-flink - Flink:处理删除在临时视图中过滤掉的记录
- wordpress - 在 WooCommerce 客户完成订单电子邮件通知中添加基于运输方式 ID 的消息
- c - 使用 C 将值打印到文本文件