我正在开发一个购物车网站。在我的购物车页面上,商品的总价应根据该商品的数量进行更新。https://github.com/darryldecode/laravelshoppingcart我安装了这个购物车包。
如何根据商品数量更新商品的总价?
购物车控制器.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
use App\Category;
use Darryldecode\Cart\Cart;
class CartController extends Controller
{
public function index()
{
// $cartItems = \Cart::session(auth()->id())->getContent();
return view ('cart');
}
public function show($id)
{
$product = Product::find($id);
return view('cart')->with(compact('product'));
}
public function update($rowId)
{
\Cart::session(auth()->id())->update($rowId, [
'quantity' => [
'relative' => true,
'value'=> request('quantity')
]
]);
return back();
}
public function destroy($itemId)
{
\Cart::session(auth()->id())->remove($itemId);
return back();
}
public function addtocart(Product $product)
{
\Cart::session(auth()->id())->add(array(
'id' => $product->id,
'name' => $product->prod_name,
'price' => $product->prod_price,
'quantity' => 1,
'attributes' => array(),
'associatedModel' => $product
));
return redirect()->back();
}
}
网络.php
Route::get('/cart', 'CartController@index')->name('cart.index')->middleware('auth');;
Route::get('/cart/{cartItems}', 'CartController@add')->name('cart.add')->middleware('auth');
Route::get('/cart/destroy/{itemId}', 'CartController@destroy')->name('cart.destroy')->middleware('auth');
Route::get('/cart/update/{itemId}', 'CartController@update')->name('cart.update')->middleware('auth');
Route::get('/add-to-cart/{product}','CartController@addtocart')->name('addToCart');
回首忆惘然
HUX布斯
摇曳的蔷薇