繁体   English   中英

Laravel - 具有多个值的多个复选框折扣,进入数据库

[英]Laravel - Multiple checkbox Discounts with multiple value, into database

我的下拉列表:

@foreach ($discounts as $discount)
<div class="form-check">
<label class="form-check-label" for="{{ $discount->id }}">
<input name="discount[]" class="form-check-input" type="checkbox" value="{{$discount->name}}|{{$discount->amount}}" id="{{ $discount->id }}">
{{ $discount->name }} (Minus ${{ $discount->amount }})
</label>
</div>
@endforeach

我提交的数据:

这个 foreach 循环从数据库中获取,并在每个复选框的值中给出名称和数量

我试过内爆并得到了这个:

军事|50,回头客|50,推荐|100

或者实际上网站用户决定拥有的数量和名称......

但我需要做的是:

分别获取名称和总数,这样我就可以制作一串:

军事,回头客,推荐

和单独的字符串:

50, 50, 100

弄清楚了。 我将单选按钮的值更改为 $discount->id,然后执行以下操作:

$discounts = $request->input('discount');
            if($discounts==true){
                
                foreach($discounts as $discount){
                    $findDiscounts = Discount::where('id', $discount)->first();
                    
                    $discount_names_array[] = $findDiscounts->name;
                    $discount_amounts_array[] = $findDiscounts->amount;
                    $discount_together_array[] = $findDiscounts->name.' ($'.$findDiscounts->amount.')';
                }
                $buyerDiscounts = implode(', ', $discount_together_array); //gives us a nice layout like: Returning Client ($100), Military ($50)
                $buyerDiscountsTotal = array_sum($discount_amounts_array); //gives us total of all discounts in the array, example: $150
            }else{
                $buyerDiscounts = null;
                $buyerDiscountsTotal = null;
            }

现在我只需调用 $buyerDiscounts 或 $buyerDiscountsTotal 就可以非常轻松地获得它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM