如何解决带有 json 数据 laravel 的 Foreach 循环
我想用laravel excel用json数据在html中制作一个表格,数据看起来像这样
4 => {
+"id": 5
+"date": "2020-1-3"
+"grade": "7"
+"nama": "["bryan","anderson"]"
+"status": "["active","active"]"
}
我希望的 excel 文件输出是 this。 状态根据日期填充列,如果没有特定日期的数据,列将填充“-”。我尝试了在这里找到的 foreach 循环,但没有任何运气。
我用 $reports
调用数据,例如 $reports->status
将返回 ["active","active"]
,依此类推。
这是我正在处理的代码
<table>
<thead>
<tr>
<th>No.</th>
<th>Name</th>
@for ($i = 1; $i < 32; $i++)
<th>{{ $i }}</th>
@endfor
</tr>
</thead>
<tbody>
@foreach ($reports as $rp)
<tr>
<td>{{ $loop->iteration }}</td>
<!--INSERT DATA HERE-->
<td></td>
</tr>
@endforeach
</tbody>
</table>
dd($reports) 返回 =
array:2 [▼
0 => {#455 ▼
+"id": 17
+"date": "2021-01-03"
+"grade": "7"
+"nama": "["bryan","active"]"
+"created_at": "2021-01-23 05:33:27"
+"updated_at": "2021-01-23 05:33:27"
}
1 => {#1414 ▶}
]
解决方法
Try this following code;
<table border=1>
<thead>
<tr>
<th>No.</th>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
@php $index=1; @endphp
@foreach ($reports as $rp)
@foreach ($rp->nama as $key => $val)
<tr>
<td>{{ $index }}</td>
<td>{{ $val }}</td>
<td>{{ optional($rp->status)[$key] }}</td>
</tr>
@php $index++; @endphp
@endforeach
@endforeach
</tbody>
</table>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。