两人一组输出Laravelcollection
Output Laravel collection in groups of two
我正在努力将 table 拆分为 Laravel blade 中的两列。例如,如果我有 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 中做到这一点?
您可以使用 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
请注意,如果 $idAndProducts
是 collection then array_chunk
will not work. You will need to use the inbuilt method chunk
。只需将第一行更新为 read
@foreach ( $idAndProducts->chunk(2) as $row )
我正在努力将 table 拆分为 Laravel blade 中的两列。例如,如果我有 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 中做到这一点?
您可以使用 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
请注意,如果 $idAndProducts
是 collection then array_chunk
will not work. You will need to use the inbuilt method chunk
。只需将第一行更新为 read
@foreach ( $idAndProducts->chunk(2) as $row )