php - 在 Laravel 中为新行添加 div 行
问题描述
我是 Laravel 的初学者。我在 Laravel 8 中制作我的项目。
我有这个代码:
@foreach($productIngredients as $productIngredient)
@php
if($selectedProductIngredients->contains('ingredient_id', $productIngredient->id) === true){
$item = \App\Models\SelectedProductIngredient::where('product_id', $product->id)->where('ingredient_id', $productIngredient->id)->first();
$weight = $item->weight;
} else {
$weight = null;
}
@endphp
<div class="col-6">
<div class="form-check py-2">
<input id="productIngredientId-{{ $productIngredient->id }}"
class="form-check-input enableInput" style="margin-top:10px"
name="productIngredient[]" type="checkbox"
value="{{ $productIngredient->id }}"
@if($selectedProductIngredients->contains('ingredient_id', $productIngredient->id) === true) checked @endif>
<label class="form-check-label" for="flexCheckChecked">
{{ $productIngredient->name }} [{{ $productIngredient->short_name }}]
</label>
<input id="productIngredient-{{ $productIngredient->id }}" type="text"
name="productIngredient-{{ $productIngredient->id }}" maxlength="10"
class="form-control weight-input weightMask"
style="width:100px;display: inline; margin-left:20px" placeholder=""
value="{{ $weight }}">
</div>
</div>
@endforeach
它工作正常。
我希望 1 行中有 2 条记录。所以我想在最后一条记录的情况下添加并关闭 div
我怎样才能做到这一点?
请帮我。
解决方案
你知道Laravel 的集合吗?它可以帮助你。
您可以使用chunk
方法,它将集合分成多个给定大小的较小集合:
@foreach($productIngredients->chunk(2) as $chunk)
<div class="row">
@foreach($chunk as $productIngredients)
<div class="col-6">
...
</div>
@endforeach
</div>
@endforeach
推荐阅读
- python - 如果列名在列表中,则将数据框中的列一起添加
- azure - Azure Active Directory 撤销给定 aad app-id 的一组用户会话
- pandas - 在另一列熊猫上删除具有特定条件的重复值
- discord - 如何使用 python3 存储不和谐成员?
- perl - 如何在 Windows 上使用 cpan 为 Apache httpd 2.4.41 (xampp) 安装 mod_perl?
- node.js - 尝试在 Google Cloud Functions 中删除文档时收到内部错误
- angular - 我如何用 ngstyle 解决问题?
- javascript - 无法将 Firebase 存储下载 URL 传递给接口属性
- cadence-workflow - 如何从子工作流重新启动父工作流,或者我可以在父工作流本身内重新触发?- 节奏/时间
- azure - Azure Functions - 拥有 host.json 和 local.settings.json 的目的是什么