【发布时间】:2021-02-16 13:17:08
【问题描述】:
这是一个购物车,数量乘以价格显示小计,当我只在购物车中输入一个项目时,它可以工作,但是当我输入多个项目时,它会出现此错误。
完整性约束违规:1048 列“数量”不能为空(SQL:更新carts 设置quantity = ?)”
我相信它来自这个 $("#qty").change(function(e) Id 采取了一项,但我不确定,我们将非常感谢您的帮助
刀片:
<tr>
<td class="product-thumbnail">
<a href="#"><img style="object-fit: contain;" src="{{asset('product-images/'.$product->image)}}" alt="" width="200px" height="120px"></a>
</td>
<td class="product-name">{{$product->title}}</td>
<td class="product-price-cart"><span class="amount" id="price">{{$product->price}} </span> $</td>
<td class="product-quantity ">
<div class="cart-plus-minus ">
<input class="cart-plus-minus-box qty" type="number" id="qty" class="qty" name="qty" value="1" >
</div>
</td>
<td class="product-subtotal" id="subtotal"><span class="subtotal">{{$product->price}} </span>$ </td>
<td>
<form action="{{route('cart.destroy',$cart->id)}}" method="POST" >
@csrf
@method('DELETE')
<button class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
刀片中的脚本:
$( document ).ready(function() {
$("#qty").change(function(e){
e.preventDefault();
var selct_ = $(this) //declare this
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: "{{ route('checkout.qty') }}",
data:{
'quantity':qty.value
},
type: "get",
success: function(result){
var price=selct_.closest('tr').find('.amount').text();
console.log(price);
$('.subtotal').text(result[0]*price);
}
});
});
});
控制器
public function quantity(Request $request){
DB::table('carts')->update(
['quantity'=> $request->quantity]
);
return response()->json([$request->quantity,200]);
}
【问题讨论】: