vendredi 4 janvier 2019

Updating Database Field with AJAX Laravel

I am trying to update my stock level by subtracting the cart item quantity from the product quantity in the database when a user completes an order using the POST method. Everytime I run the method the success function occurs but the field doesnt update doesnt update.

Could anyone tell me why?

My Controller method:

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;
    }
}

My Route:

Route::post('stock', 'ProductController@stock');

My view 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"></h4>
                        </div>
                    </div>
                </td>
                <td data-th="Price">$</td>
                <td data-th="Quantity">
                    <input type="number" value="" class="form-control quantity" />
                </td>
                <td data-th="Subtotal" class="text-center">$</td>
                <td class="actions" data-th="">
                    <button class="btn btn-info btn-sm update-cart" data-id=""><i class="fa fa-refresh"></i></button>
                    <button class="btn btn-danger btn-sm remove-from-cart" data-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 </strong></td>
    </tr>
    <tr>
        <td><a href="" 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 $</strong></td>
    </tr>
    </tfoot>

    <div class="row">
       <div class="btn col-md-12">
           <a href="" id="order-complete">Test</a>
       </div>

    </div>
</table>

<script type="text/javascript">

    $("#order-complete").click(function (e){
       e.preventDefault();

        var ele = $(this);

        $.ajax({
           url: '',
           method: "post",
           data: {_token: ''},
           success: function () {

            window.location.reload();
           }
        });
    });
</script>

@endsection



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2COF7T0
via IFTTT

Aucun commentaire:

Enregistrer un commentaire