所以我正在处理我需要打印的优惠券。我准备了视图,我正在将优惠券传递给视图:
public function printSelected($ids){
$couponIDs = explode(',', $ids);
$selectedCoupons = Coupon::find($couponIDs);
return view('admin.coupons.print')->with(compact('selectedCoupons'));
}
现在我需要以某种方式遍历它们。
答案 0 :(得分:1)
有多种方法可以做到这一点,但我发现使用chunk
对象的Collection
方法非常易读。另一种选择是使用模数($index % 10 === 0
)。
<div class="coupons">
{{-- Create chunks of 10 as this is the max per page --}}
@foreach($selectedCoupons->chunk(10) as $pageCoupons)
<div class="page">
<div class="subpage">
<table>
{{-- Create chunks of 2 so every row is filled with up to 2 coupons --}}
@foreach($pageCoupons->chunk(2) as $rowCoupons)
<tr>
{{-- Loop over the row coupons to create the columns --}}
@foreach($rowCoupons as $coupon)
<td>
<span class="coupon">{{ $coupon->code }}</span><br>
<hr>
<span class="name">{{ $coupon->user->name }}</span>
</td>
@endforeach
</tr>
@endforeach
</table>
</div>
</div>
@endforeach
</div>
答案 1 :(得分:0)
您可以使用该集合的chunk
方法循环优惠券的块:
<div class="coupons">
@foreach ($selectedCoupons->chunk(10) as $page)
<div class="page">
<div class="subpage">
<table>
@foreach ($page->chunk(2) as $row)
<tr>
@foreach ($row as $coupon)
<td>
<span class="coupon">{{ $coupon->code }}</span><br>
<hr>
<span class="name">{{ $coupon->user->name }}</span>
</td>
@endforeach
</tr>
@endforeach
</table>
</div>
</div>
@endforeach
</div>