块方法不适用于刀片Laravel

时间:2020-05-28 05:47:41

标签: laravel

我必须将结果显示为刀片中的五个块,这是我的代码:

 <?php
                        $iterationNo = 0;
                        ?>
                        @foreach($purchase->orders as $order)
                        @foreach($order->items->chunk(5) as $item)

                        <tr style="font-size: 10pt;">
                            <td style="padding: 16px; text-align: center; vertical-align: top;">
                                <?php
                                $iterationNo = $iterationNo + 1;

                                ?>
                                {{ $iterationNo }}
                            </td>
                            <td style="padding: 16px; vertical-align: top;">
                                {{ $item->product->parentProduct->product_code }}
                            </td>
                            <td style="padding: 6px; vertical-align: top;">
                                <table>
                                    <tr style="font-size: 10pt;">
                                        <td style="padding: 4px; vertical-align: top;">
                                            <img src="{{ asset('storage/' . $item->product->parentProduct->images[0]->path . '/' . $item->product->parentProduct->images[0]->filename) }}" alt="{{ $item->product->parentProduct->name }}" style="width: 105px; height: 90px; border-radius: 10px;">
                                        </td>
                                        <td style="padding: 14px; vertical-align: top;">
                                            <p style="margin-bottom: 10px;">
                                                {{ $item->quantity }} x {{ $item->product->parentProduct->name }}
                                            </p>
                                            <p style="margin: 0;">
                                                @if(array_key_exists('product_color_name', $item->product_information))
                                                Color: {{ $item->product_information['product_color_name'] }}
                                                @endif
                                                @if(array_key_exists('product_size', $item->product_information))
                                                Color Temperature: {{ $item->product_information['product_size'] }}
                                                @endif
                                                @if(array_key_exists('product_temperature', $item->product_information))
                                                Color Temperature: {{ $item->product_information['product_temperature'] }}
                                                @endif
                                            </p>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td style="padding: 16px; text-align: center; vertical-align: top;">
                                {{ $item->quantity }}
                            </td>
                            <td style="padding: 16px; text-align: center; vertical-align: top;">
                                {{ $item->product->getDecimalPrice() }}
                            </td>
                            <td style="padding: 16px; text-align: center; vertical-align: top;">
                                {{ number_format(($item->subtotal_price / 100), 2) }}
                            </td>
                        </tr>

                        @endforeach
                        @endforeach
                    </table>

它显示错误:

此集合实例中不存在属性[product]。

不确定使用chunk()时为什么会发生这种情况。尝试将其替换为take(),但也没有显示正确的结果。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

chunk方法返回如下数组: [collection([item1, item2, item3...]), collection([itemx, itemy, itemz...])] 因此,您的$ item将获得一个包含前5个项目的集合。

您必须在其中再添加一个foreach,以遍历那些分块的项目

foreach($order->items->chunk(5) as $items) {
    foreach($items as $item) {
        //magic inside
    }
}