如何在 laravel blade 表中循环数据?

How to loop data inside laravel blade tables?

我在 laravel blade 文件中有这个 table。

@foreach($items as $item)
<table class="pack-table">
    <tr>
        <th width="30%">Color</th>
        <th width="10%">Pack</th>
        <th width="60%">Total Units</th>
    </tr>
    <tr>
        <td>
            @foreach($item['grid'] as $color => $sizes)
                {{$color}}<br><br>
            @endforeach
        </td>
        <td>
            @foreach ($item['grid'] as $color => $sizes)
                @foreach ($sizes as $size)
                    {{ $size['description'] }}<br><br>
                @endforeach
            @endforeach
        </td>
        <td>
            @foreach ($item['grid'] as $color => $sizes)
                @foreach ($sizes as $size)
                    {{ $size['total'] }}<br><br>
                @endforeach
            @endforeach
        </td>
    </tr>
</table>
@endforeach

此代码打印如下图所示的 table。

这只是打印每个数据。但我希望将其打印如下: 例如颜色,VELBL 的尺码为 MEDIUM、X LARGE、LARGE、X SMALL 和 SMALL,所有这些包装尺寸的总单位数为 0.

所以我需要先打印所有这些记录,然后打印下一种颜色 BLUAS,它是包装尺寸,以及每种颜色的总数。

如何修改我的代码以按要求打印?

更新:

<table class="pack-table">
    <tr>
        <th width="30%">Color</th>
        <th width="10%">Pack</th>
        <th width="60%">Total Units</th>
    </tr>
    @foreach($item['grid'] as $color => $sizes)
    <tr>
        <td colspan="3" align="left">{{ $color }}</td>
    </tr>
    @foreach ($sizes as $size)
    <tr>
        <td>{{ $size['description'] }}</td>
        <td>{{ $size['total'] }}</td>
    </tr>
    @endforeach
    @endforeach
</table>

这就是您正在寻找的循环更少的东西

@foreach($items as $item)
<table class="pack-table">
    <tr>
        <th width="30%">Color</th>
        <th width="10%">Pack</th>
        <th width="60%">Total Units</th>
    </tr>
    @foreach($item['grid'] as $color => $sizes)
    <tr>
        <td colspan="{{count($sizes)+1}}">
            {{$color}}
        </td>
        <?php $totals = 0 ?>
        @foreach ($sizes as $size)
        <td>
            {{ $size['description'] }}
        </td>
        <td>
            <?php $totals += $size['total']  ?>
            {{ $size['total'] }}
        </td>
    </tr>
    <tr>
        @endforeach
        <td>TOTAL</td>
        <td>{{$totals}}</td>
    </tr>
    @endforeach
</table>
@endforeach

使用此代码

@foreach($items as $item)
    <table class="pack-table">
        <tr>
            <th width="30%">Color</th>
            <th width="10%">Pack</th>
            <th width="60%">Total Units</th>
        </tr>
        @foreach($item['grid'] as $color => $sizes)
            <tr>
                <td colspan="3" align="left">{{ $color }}</td>
            </tr>
            @foreach ($sizes as $size)
                <tr>
                    <td></td>
                    <td>{{ $size['description'] }}</td>
                    <td>{{ $size['total'] }}</td>
                </tr>
            @endforeach
        @endforeach
    </table>
@endforeach

我觉得是这样的:

@foreach($items as $item)
    <table class="pack-table">
        <tr>
            <th width="30%">Color</th>
            <th width="10%">Pack</th>
            <th width="60%">Total Units</th>
        </tr>
        @foreach($item['grid'] as $color => $sizes)
            <tr>
                <td>{{ $color }}</td>
                <td>
                    <table>
                    <?php $total = 0; ?>
                    @foreach ($sizes as $size)
                        <tr>
                            <td>{{ $size['description'] }}</td>
                        <tr>
                        <?php $total += $size['total'] ?>
                    @endforeach
                    </table>
                </td>
                <td>{{ $total }}</td>
            </tr>
        @endforeach
        
    </table>
@endforeach