简体   繁体   English

Laravel 购物车更新数量 inc-desc 包(darryldecode)

[英]Laravel Shopping Cart Update quantity inc-desc Package(darryldecode)

I want to increase or decrease the quantity on the cart page, but when I try to increase or decrease the quantity it adds to the amount it is taking.我想增加或减少购物车页面上的数量,但是当我尝试增加或减少数量时,它会增加它正在使用的数量。

Sample;样本;

Quantity jumps from 1 to 3. So $cartItems += 1数量从 1 跳到 3。所以$cartItems += 1

In short it doesn't work on increasing or decreasing how can I increase and decrease one by one?简而言之,它对增加或减少不起作用我怎样才能一一增加和减少?

Following my code: app.js按照我的代码: app.js

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

$('.qty-inc, .qty-desc').on('click',function () {
    var id =$(this).attr('data-id');
    var qty =$(this).attr('data-qty');
    $.ajax({
        type:'PATCH',
        url:'basket/update/'+id,
        data:{qty:qty},
        success:function () {
        window.location.href='/basket';
        }
    });
});

Route

Route::patch('basket/update/{userID}','App\Http\Controllers\BasketController@update')->name('basket.update');

Controller

use Cart;
public function update($userID){
  Cart::update($userID,[
   'quantity' => request('qty')
   ]);

   return response()->json(['success' => true]);
}

basket.blade.php

@foreach(Cart::getContent() as $cartItems)

<a href="#" class="qty-desc" data-id="{{ $cartItems->id }}" data-qty="{{$cartItems->quantity-1}}">-</a>

<span style="padding: 10px 20px">{{ $cartItems->quantity }}</span>

<a href="#" class="qty-inc" data-id="{{ $cartItems->id }}" data-qty="{{$cartItems->quantity+1}}">+</a>

@endforeach

I found the solution, maybe I'm sharing it in case someone needs it.我找到了解决方案,也许我正在分享它以防有人需要它。 Controller

Cart::update($userID,[
    'quantity' => array(
     'relative' => false,
     'value' => request('qty')
     ),
 ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM