当用户使用POST方法完成订单时,我试图通过从数据库中的产品数量中减去购物车项目数量来更新我的库存水平。每次我运行该方法时,都会发生成功功能,但是该字段不更新也不更新。
谁能告诉我为什么?
我的控制器:
public function index ()
{
$products = Product::all();
return view('products', compact('products'));
}
public function cart()
{
return view('cart');
}
public function addToCart($id)
{
$product = Product::find($id);
if(!$product) {
abort(404);
}
$cart = session()->get('cart');
// if cart is empty then this will be the first product
if(!$cart) {
$cart = [
$id => [
"name" => $product->name,
"quantity" => 1,
"price" => $product->unit_price
]
];
session()->put('cart', $cart);
return redirect()->back()->with('success', 'Product added to cart successfully!');
}
// if cart isnt empty then check if this product exist then increment quantity
if(isset($cart[$id])) {
$cart[$id]['quantity']++;
session()->put('cart', $cart);
return redirect()->back()->with('success', 'Product added to cart successfully!');
}
// if item doesnt exist in cart then add to cart with quantity = 1
$cart[$id] = [
"name" => $product->name,
"quantity" => 1,
"price" => $product->unit_price
];
session()->put('cart', $cart);
return redirect()->back()->with('success', 'Product added to cart successfully!');
}
public function update(Request $request)
{
if($request->id and $request->quantity)
{
$cart = session()->get('cart');
$cart[$request->id]["quantity"] = $request->quantity;
session()->put('cart', $cart);
session()->flash('success', 'Cart updated successfully');
}
}
public function remove(Request $request)
{
if($request->id) {
$cart = session()->get('cart');
if(isset($cart[$request->id])) {
unset($cart[$request->id]);
session()->put('cart', $cart);
}
session()->flash('success', 'Product removed successfully');
}
}
public function stock (Request $request)
{
if($request->id and $request->quantity)
{
$cart = session()->get('cart');
$cart[$request->id]['quantity'] = $request->quantity;
$products = Product::all();
$stock = $products->unit_stock;
$quantity = $stock - $cart;
return $quantity;
}
}
我的路线:
Route::post('stock', 'ProductController@stock');
我的视图cart.blade.php:
@extends('layout')
@section('content')
<table id="cart" class="table table-hover table-condensed">
<thead>
<tr>
<th style="width:50%">Product</th>
<th style="width:10%">Price</th>
<th style="width:8%">Quantity</th>
<th style="width:22%" class="text-center">Subtotal</th>
<th style="width:10%"></th>
</tr>
</thead>
<tbody>
<?php $total = 0 ?>
@if(session('cart'))
@foreach(session('cart') as $id => $details)
<?php $total += $details['price'] * $details['quantity'] ?>
<tr>
<td data-th="Product">
<div class="row">
<div class="col-sm-9">
<h4 class="nomargin">{{ $details['name'] }}</h4>
</div>
</div>
</td>
<td data-th="Price">${{ $details['price'] }}</td>
<td data-th="Quantity">
<input type="number" value="{{ $details['quantity'] }}" class="form-control quantity" />
</td>
<td data-th="Subtotal" class="text-center">${{ $details['price'] * $details['quantity'] }}</td>
<td class="actions" data-th="">
<button class="btn btn-info btn-sm update-cart" data-id="{{ $id }}"><i class="fa fa-refresh"></i></button>
<button class="btn btn-danger btn-sm remove-from-cart" data-id="{{ $id }}"><i class="fa fa-trash-o"></i></button>
</td>
</tr>
@endforeach
@endif
</tbody>
<tfoot>
<tr class="visible-xs">
<td class="text-center"><strong>Total {{ $total }}</strong></td>
</tr>
<tr>
<td><a href="{{ url('/products') }}" class="btn btn-warning"><i class="fa fa-angle-left"></i> Continue Shopping</a></td>
<td colspan="2" class="hidden-xs"></td>
<td class="hidden-xs text-center"><strong>Total ${{ $total }}</strong></td>
</tr>
</tfoot>
<div class="row">
<div class="btn col-md-12">
<a href="{{ url('/cart') }}" id="order-complete">Test</a>
</div>
</div>
</table>
<script type="text/javascript">
$("#order-complete").click(function (e){
e.preventDefault();
var ele = $(this);
$.ajax({
url: '{{ url('stock') }}',
method: "post",
data: {_token: '{{ csrf_token() }}'},
success: function () {
window.location.reload();
}
});
});
</script>
@endsection
答案 0 :(得分:0)
我可以看到可能导致此问题的几个潜在问题。首先,看起来您正在尝试通过加载包含所有产品的集合来一次性设置数据库中所有产品的库存,而不是循环/加载订单中包含的产品($ request)。您在这里执行此操作;
$products = Product::all();
然后,您尝试在此处更改该系列中所有产品的库存;
$stock = $products->unit_stock;
$quantity = $stock - $cart;
我想您应该在$cart
变量中包含一组产品,并应对其进行循环并加载以进行操作。一些伪代码来说明我的观点;
foreach($product in $cart){
$loadedProduct = Product::find($product);
$loadedProduct->stock = $loadedProduct->stock - $product["quantity"];
$loadedProduct->save();
}
您也不会在提供的代码中保存任何产品。上面的伪代码中有一个示例。
答案 1 :(得分:0)
我可以从您的代码中发现一些错误。
此行告诉我,有数据id
和quantity
正在发送。
if($request->id and $request->quantity)
从您的路线的外观来看,它位于身体中。但是在ajax函数中,除了csrf令牌之外,没有包含任何数据。尝试添加id
和quantity
数据。这只是该值的假设。
data: {_token: '{{ csrf_token() }}', id: 6, quantity: 2},
第二,此函数返回产品的集合。
$products = Product::all();
因此,如果您要修改产品,则必须访问其索引。例如
$products[0]->unit_stock = 3;
$products[0]->save();
或者刘易斯所说的,您可以使用foreach循环来迭代集合中的每个对象