Blade 模板布局结构
Blade Template Layout Structure
我需要为每 3 个附件创建一行。像
@foreach($email->attachment as $attach)
<div class="row">
<div class="col-lg-4"></div>
<div class="col-lg-4"></div>
<div class="col-lg-4"></div>
</div>
@endforeach
<div class="row">
<div class="col-lg-4">1st attachment</div>
<div class="col-lg-4">2nd attachment</div>
<div class="col-lg-4">3rd attachment</div>
</div>
<div class="row">
<div class="col-lg-4">4th attachment</div>
<div class="col-lg-4">5th attachment</div>
<div class="col-lg-4">6th attachment</div>
</div>
等等!我已经搜索了这个问题,但只找到 This link 但它实际上与我的观点不同。
谢谢
Use array_chunk - 它是为每个目的而设计的 - 所以你可以将数组切割成子大小并循环遍历每个组
@foreach (array_chunk($email->attachment->toArray(), 3, true) as $array)
<div class="row">
@foreach($array as $attachment)
<div class="col-lg-4">{{ $attachment['id'] }}</div>
@endforeach
</div>
@endforeach
并使用@Lukasgeiter 的评论 - 如果它是 Laravel Collection
,您也可以这样做
@foreach ($email->attachment->chunk(3) as $array)
<div class="row">
@foreach($array as $attachment)
<div class="col-lg-4">{{ $attachment->id }}</div>
@endforeach
</div>
@endforeach
试试这个,它比生成 2 个数组更快:
<div class="row">
@foreach( $email->attachment as $index => $attach)
<div class="col-lg-4">{{$attach}}</div>
@if( $index+1 === 0)
</div><div class="row">
@endif
@endforeach
</div>
我需要为每 3 个附件创建一行。像
@foreach($email->attachment as $attach)
<div class="row">
<div class="col-lg-4"></div>
<div class="col-lg-4"></div>
<div class="col-lg-4"></div>
</div>
@endforeach
<div class="row">
<div class="col-lg-4">1st attachment</div>
<div class="col-lg-4">2nd attachment</div>
<div class="col-lg-4">3rd attachment</div>
</div>
<div class="row">
<div class="col-lg-4">4th attachment</div>
<div class="col-lg-4">5th attachment</div>
<div class="col-lg-4">6th attachment</div>
</div>
等等!我已经搜索了这个问题,但只找到 This link 但它实际上与我的观点不同。 谢谢
Use array_chunk - 它是为每个目的而设计的 - 所以你可以将数组切割成子大小并循环遍历每个组
@foreach (array_chunk($email->attachment->toArray(), 3, true) as $array)
<div class="row">
@foreach($array as $attachment)
<div class="col-lg-4">{{ $attachment['id'] }}</div>
@endforeach
</div>
@endforeach
并使用@Lukasgeiter 的评论 - 如果它是 Laravel Collection
,您也可以这样做@foreach ($email->attachment->chunk(3) as $array)
<div class="row">
@foreach($array as $attachment)
<div class="col-lg-4">{{ $attachment->id }}</div>
@endforeach
</div>
@endforeach
试试这个,它比生成 2 个数组更快:
<div class="row">
@foreach( $email->attachment as $index => $attach)
<div class="col-lg-4">{{$attach}}</div>
@if( $index+1 === 0)
</div><div class="row">
@endif
@endforeach
</div>