Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

excel - Foreach loop with json data laravel

I want to make a table in html with json data using laravel excel, the data looks like this

4 => {
    +"id": 5
    +"date": "2020-1-3"
    +"grade": "7"
    +"nama": "["bryan","anderson"]"
    +"status": "["active","active"]"
}

the excel file output i was hoping for is this. Status fill the column based on dates and if there is no data in particular date the column will filled with "-". I tried a foreach loop I found here, but did not have any luck.

I call the data with $reports, for example $reports->status will return ["active","active"] and so on.

This is the code i am working on

<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) return =

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 ?}
]
question from:https://stackoverflow.com/questions/65859913/foreach-loop-with-json-data-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...