以两个为一组输出Laravel集合

时间:2017-03-21 11:29:12

标签: php jquery laravel laravel-blade

我正在努力将桌子分成Laravel刀片中的两列。例如,如果我有17行,我想在第一列显示其中的9行,其余的在第二列中显示。这是我的代码:

@foreach($idAndProducts as $id)
<tr>
<td width="15px">
<input type="checkbox" id="products{{$id->product_id}}" name="products[]" value="{{$id->product_id}}">
<input type="hidden" name="campaignID[]" value="{{$id->id}}"></td>
<td width="480px"><label for="products{{$id->product_id}}">{{$id->products}}</label></td>
</tr>
@endforeach

有没有办法在Blade中执行此操作或使用jQuery?

1 个答案:

答案 0 :(得分:2)

您可以使用array_chunk将它们分成两组。试试这个:

@foreach ( array_chunk($idAndProducts, 2) as $row )
    <tr>
    @foreach ( $row as $id )
        <td width="15px">
            <input type="checkbox" id="products{{$id->product_id}}" name="products[]" value="{{$id->product_id}}">
            <input type="hidden" name="campaignID[]" value="{{$id->id}}">         
        </td>
        <td width="480px">
            <label for="products{{$id->product_id}}">{{$id->products}}</label>
        </td>
    @endforeach
    </tr>
@endforeach

请注意,如果$idAndProductscollection,那么array_chunk将无效。您需要使用内置方法chunk。只需更新第一行即可阅读

@foreach ( $idAndProducts->chunk(2) as $row )