1

I'm working on a shopping cart with Laravel.

I have :

Routes :

Route::post('/panier/ajouter', 'CartController@store')->name('cart.store');
Route::patch('/panier/{product}', 'CartController@update')->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 request :

$("#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);
        });
    })
});

I need to get the quantity in my controller so i try :

public function update(Request $request)
    {
        $data = $request->json()->all();
        Log::info($data);
        return response()->json(['success' => 'Cart Quantity Has Been Updated']);
    }

But when i try to get a $data value, my array is empty like this in my log :

[2020-02-22 10:49:46] local.INFO: array ()  

I tryed to change :

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

To

$data = $request->all();

But same problem.

Do you have any idea ?

Thanks !

Guetguet
  • 27
  • 7
  • Could you plz add your route file ? – Foued MOUSSI Feb 22 '20 at 11:04
  • oh yes sorry ! Its done – Guetguet Feb 22 '20 at 11:08
  • I think that because `axios.patch` is now working properly refer this https://stackoverflow.com/questions/51170564/axios-patch-axios-put-is-not-working-in-vue-and-laravel – Chamara Abeysekara Feb 22 '20 at 11:11
  • 1
    I tryed this. Same error but i see in my array the row_method but not quantity. So i tryed to change quantity:this.value to quantity:5 to test. And i get the row quantity now. So this problem is quantity:this.value in ajax request. Do you have an idea ? – Guetguet Feb 22 '20 at 11:21
  • `this` is referring local scope in JS . since you are using Jquery I don't see the point of using `this`. you need to inject the event to you change function and from there you can get the value. let me know if need you need post it answer – Chamara Abeysekara Feb 22 '20 at 11:40

1 Answers1

1

Laravel cheats with PUT/PATCH/DELETE etc requests. These need to be POST requests with the extra variable '_method' set to the request type (e.g.) PATCH.

Foued MOUSSI
  • 4,643
  • 3
  • 19
  • 39
  • Now its working but my on change working only the first time ? – Guetguet Feb 22 '20 at 11:32
  • Use `$('body').on('change','#quantity',function(){...});`instead – Foued MOUSSI Feb 22 '20 at 11:40
  • ooh you are the boss !! Juste the problem its when i have more then one product in my cart. When i change the quantity on the first product, the second product quantity change too. How i can fix this ? – Guetguet Feb 22 '20 at 11:48
  • Glad to help, if this answer solved your problem please mark it as accepted by clicking the check mark next to the answer. – Foued MOUSSI Feb 22 '20 at 12:19