arrays - 需要帮助使用 @foreach 和关系模型将数组值显示到刀片中
问题描述
我有 4 种订阅类型(每月、3 个月、6 个月和每年)。我成功插入了与我的产品模型相关的值。现在我需要进行更新。场景是用户一开始只选择两个订阅计划,然后决定添加更多订阅。在我的编辑中它只显示用户选择的内容。
@foreach($product->subscriptions as $subscription)
<input class="form-check-input" name="availability[]" type="checkbox" value="{{ $row->id }}" checked>
<label class="form-check-label" for="materialUnchecked"><b>{{ $subscription->name }}</b></label>
<input type="text" name="price[]" value="{{ $subscription->pivot->price }}">
@endforeach
如果结果小于 4,我想显示未选中的其余订阅可用性。例如,用户仅选择每月和每年。现在在我的编辑视图中,我还想显示另外两个(3 个月和 6 个月选项),以防他们想添加一些东西。怎么做?
解决方案
@foreach($subscriptions as $subscription)
<input class="form-check-input" name="availability[]" type="checkbox" value="{{ $row->id }}" {{in_array($subscription->id, $product->subscriptions->pluck('id')) ? 'checked' : ''}}>
<label class="form-check-label" for="materialUnchecked"><b>{{ $subscription->name }}</b></label>
<input type="text" name="price[]" value="{{ $subscription->pivot->price }}">
@endforeach
推荐阅读
- reactjs - React redux Sagas, wait for a Saga to set state
- codenameone - Are there open source codename projects repos
- clojure - How to Access Environment Variables In Clojure - Luminus Web Framework
- reactjs - React-hook-form with Material-ui textfield without autoFocus defaultValue 在表单提交期间消失了
- .net - 如何使用 HotChocolate 和 Dapper 根据 GraphQL 请求自定义 SQL 查询?
- react-admin - 如何在 react-admin 中包含 DataGrid 或 SimpleForm 中的对象?
- javascript - 错误 TableExistsException:无法使用 Mikro-ORM 和 PSQL 创建迁移
- .net - Protobuf:重复应该发出集合访问器
- python - 避免在 PyTorch 的批量张量中选择维度的循环
- python - 为什么获取 PostForm' 对象没有属性'用户?