Я пытаюсь обновить уровень своего запаса, вычитая количество товара в корзине из количества товара в базе данных, когда пользователь выполняет заказ с использованием метода 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