需要帮助使用@foreach和关系模型将数组值显示到刀片中

问题描述 投票:0回答:1

我有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

这将产生结果:如何显示之前未选择的其他选项。enter image description here

如果结果小于4,我想不显示剩余的订阅可用性。例如,用户仅选择每月和每年。现在,在我的编辑视图中,我想同时显示其他两个(3个月和6个月的选项),以防他们想要添加一些内容。怎么做?

arrays laravel checkbox foreach relation
1个回答
0
投票
 @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
© www.soinside.com 2019 - 2024. All rights reserved.