Home » Jquery » javascript – PATCH 500 (Internal Server Error) on Laravel route

javascript – PATCH 500 (Internal Server Error) on Laravel route

Posted by: admin February 22, 2020 Leave a comment

Questions:

I’m working on a shopping cart with Laravel.
I’ve made an update that should update the shopping cart quantity dynamically without refreshing the page.

Route : Route::patch('/panier/{product}', '[email protected]')->name('cart.update');

View :

<table class="table" id="table-shoppingcart">
                <thead>
                    <tr>
                        <th class="text-center" id="item-title-shoppingcart"></th>
                        <th class="text-center" id="size-title-shoppingcart">Taille</th>
                        <th class="text-center" id="quantity-title-shoppingcart">Quantité</th>
                        <th class="text-center" id="price-title-shoppingcart">Prix</th>
                        {{-- <th class="text-center" id="delete-title-shoppingcart"></th> --}}
                    </tr>
                </thead>
                <tbody>
                    @foreach (Cart::content() as $product)
                    <tr>

                        <th><img class="text-center item-content-shoppingcart" src="{{ $product->model->image }}"></th>
                        <td class="text-center td-table-shoppingcart size-content-shoppingcart">S</td>
                        <td class="td-table-shoppingcart quantity-content-shoppingcart">
                            <select name="quantity" class="custom-select text-center quantity" id="quantity" data-id="{{ $product->rowId }}">
                                @for ($i = 0; $i < 5 + 1 ; $i++)
                                <option id="quantity-option" {{ $product->qty == $i ? 'selected' : '' }}>{{ $i }}</option>
                            @endfor
                            </select>
                        </td>
                        <td class="text-center td-table-shoppingcart price-content-shoppingcart">{{ getPrice($product->subtotal()) }}</td>

                    </tr>
            @endforeach
                </tbody>
            </table>

Ajax :

$("#quantity").change(function(){
    const classname = document.querySelectorAll('#quantity')
    Array.from(classname).forEach(function(element) {
        console.log(element);
        let id = element.getAttribute('data-id')
        axios.patch(`/panier/${id}`, {
            quantity: this.value
        })
        .then(function (response) {
           // console.log(response);
           console.log("refresh");
            $("#table-shoppingcart").load(location.href + " #table-shoppingcart");
        })
        .catch(function (error) {
            console.log("erreur");
           // console.log(error);
        });
    })
});`

Controller :

public function update(Request $request, $rowId)
    {
        $data = $request->json()->all();

        $validator = Validator::make($request->all(), [
        'quantity' => 'required|numeric|between:0,5'
        ]);
        if( $data['quantity'] == 0){
            Cart::remove($rowId);
        }

        if($validator->fails()) {
            return response()->json(['error' => 'Cart Quantity Has Not Been Updated']);
        }

        Cart::update($rowId, $data['quantity']);
        return response()->json(['success' => 'Cart Quantity Has Been Updated']);
    }

And i got this error on each update : PATCH http://127.0.0.1:8000/panier/370d08585360f5c568b18d1f2e4ca1df 500 (Internal Server Error)

https://zupimages.net/viewer.php?id=20/08/lkcm.png

https://zupimages.net/viewer.php?id=20/08/5r2p.png

https://zupimages.net/viewer.php?id=20/08/vwo3.png

Which is the best solution ?

thank you

How to&Answer:

I’m assuming error at line: if( $data[‘quantity’] == 0){
Change:

$data = $request->json()->all();

To:

$data = $request->all();

Just like you passed to Validator::make