【问题标题】:Foreach loop with json data laravel带有 json 数据 laravel 的 Foreach 循环
【发布时间】:2021-04-27 18:59:24
【问题描述】:

我想使用 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","anderson"]"
    +"status": "["active","active"]"
    +"created_at": "2021-01-23 05:33:27"
    +"updated_at": "2021-01-23 05:33:27"
  }
  1 => {#1414 ▶}
]

【问题讨论】:

  • 您能否将dd($reports) 的输出添加到您的问题中?
  • 有问题更新

标签: json excel laravel foreach


【解决方案1】:
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>

【讨论】:

  • 代码抛出错误“Cannot use of type of stdClass as array”
  • 现在检查,错误,因为答案是集合对象而不是数组。所以,我已经更新了答案。现在,检查是否仍有问题,然后共享 dd($reports) 的输出。
  • 现在它显示“在这一行中为 foreach() 提供的参数无效“@foreach ($rp->name as $key => $val)”$rp->name 是问题所在,我不知道怎么回事
  • 请分享 dd($reports) 的全部输出,以便我为您提供帮助。
  • 有错字'name'代替'nama'。你能再试一次吗..如果仍然无法正常工作,请添加错误屏幕图像并共享示例数据(作为文本文件)以制作表格/列表
猜你喜欢
  • 2014-01-26
  • 2010-11-19
  • 2021-06-19
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 2020-03-08
  • 1970-01-01
  • 2018-03-20
相关资源
最近更新 更多