json - 带有 json 数据 laravel 的 Foreach 循环
问题描述
我想用 laravel excel 用 json 数据在 html 中制作一个表格,数据看起来像这样
4 => {
+"id": 5
+"date": "2020-1-3"
+"grade": "7"
+"nama": "["bryan","anderson"]"
+"status": "["active","active"]"
}
我希望的excel文件输出就是 这个。状态根据日期填充列,如果没有特定日期的数据,则该列将填充“-”。我尝试了一个在这里找到的 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","anderson"]"
+"status": "["active","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>
推荐阅读
- kubernetes - eksctl 在创建后应用集群更改
- r - 使用 R 根据多个条件过滤记录的优雅方法
- node.js - API 调用后未定义的响应
- ios - 我的 swift 静态框架在 OC 文件中无法使用 iphone 模拟器
- javascript - 预订应用程序,所有设备上都出现预订问题
- python - 用美丽的汤刮得到意想不到的性格
- c# - 在静态方法中创建对象时如何使用依赖注入?
- c++ - 使 \page 和 \subpage 参考 doxygen 中的方法文档
- c++ - 在 C++ 中返回不正确的楼层函数值
- linux - CentOS yum 失败并出现 libcurl.so.4 错误,提示“未定义符号:libssh2_agent_disconnect”