2017-08-25 40 views
0

。減去或抽象車數量我用這包 gloudemans /購物車
現在我有按鈕,像這樣的
enter image description here如何在Laravel

我已創建2路cartqtyminus & cartqtyplus
如何更新帶這些按鈕的購物車 按鈕有href標記

+0

'的preventDefault()'會阻止從刷新頁面的href。改爲使用ajax。 –

回答

-1

我只是這樣做了加

$rowId = $cart; 
     $product = Cart::get($rowId); 
     $productqty = $product->qty; 
     $updateqty = $productqty+1; 
     Cart::update($rowId, $updateqty); 
     return Redirect::back()->with('status', 'update Success'); 

爲零下

$rowId = $cart; 
     $product = Cart::get($rowId); 
     $productqty = $product->qty; 
     $updateqty = $productqty-1; 
     Cart::update($rowId, $updateqty); 
     return Redirect::back()->with('status', 'update Success'); 
-1

難道真的單純用increment()decrement()方法。

Cart::class爲型號,所以你可以這樣做:

Cart::findOrFail(ID)->increment('points')Cart::findOrFail(ID)->decrement('points')

詳情請閱讀Laravel documentation

現在使你的按鈕,它在發送PATCH形式(與type場)以下航線:

Route::patch('{cart}/vote', ['as' => 'cart.vote', 'uses' => '[email protected]]); 

在你的控制器就可以使方法:

public function vote(Cart $cart) 
{ 
    request()->get('type') === 'up' ? $cart->increment('points') : $cart->decrement('points'); 

    return redirect()->route('your.main.route'); 
}