我使用laravel 5.5并且我有一个在会话中存储数据的购物车到目前为止一切正常,现在我想添加color
size
等选项,每个都有不同的价格这个价格应该加到我的购物车上。
问题是我无法在购物车中获得价格。
这是我的Cart
模型中的会话存储/删除/等:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cart extends Model
{
protected $table = 'carts';
public $items = null;
public $totalQty = 0;
public $totalPrice = 0;
public function __construct($oldCart)
{
if ($oldCart) {
$this->items = $oldCart->items;
$this->totalQty = $oldCart->totalQty;
$this->totalPrice = $oldCart->totalPrice;
}
}
public function add($item, $id) {
$storedItem = ['qty' => 0, 'price' => $item->price, 'item' => $item];
if ($this->items) {
if (array_key_exists($id, $this->items)) {
$storedItem = $this->items[$id];
}
}
$storedItem['qty']++;
$storedItem['price'] = $item->price * $storedItem['qty'];
$this->items[$id] = $storedItem;
$this->totalQty++;
$this->totalPrice += $item->price;
}
public function reduceByOne($id)
{
$this->items[$id]['qty']--;
$this->items[$id]['price'] -= $this->items[$id]['item']['price'];
$this->totalQty--;
$this->totalPrice -= $this->items[$id]['item']['price'];
if ($this->items[$id]['qty'] <= 0) {
unset($this->items[$id]);
}
}
public function increaseByOne($id)
{
$this->items[$id]['qty']++;
$this->items[$id]['price'] += $this->items[$id]['item']['price'];
$this->totalQty++;
$this->totalPrice += $this->items[$id]['item']['price'];
}
public function removeItem($id)
{
$this->totalQty -= $this->items[$id]['qty'];
$this->totalPrice -= $this->items[$id]['price'];
unset($this->items[$id]);
}
protected $fillable = [
'coupon_code',
];
public function product(){
return $this->belongsTo(Product::class);
}
}
这是我的route
Route::resource('/cart', 'CartController', ['except' => ['show', 'create']]);
这是我的CartController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Cart;
use App\Product;
use Auth;
use Session;
use App\User;
class CartController extends Controller
{
public function __construct(){
$this->middleware('auth');
}
public function AddToCart(Request $request, $id)
{
$product = Product::find($id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
Session::flash('flash_message', 'Item added to your cart successfully.');
return redirect()->back();
}
public function reduceByOne($id)
{
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->reduceByOne($id);
Session::put('cart', $cart);
return redirect()->back();
}
public function increaseByOne($id)
{
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->increaseByOne($id);
Session::put('cart', $cart);
return redirect()->back();
}
public function removeItem($id)
{
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->removeItem($id);
if (count($cart->items) > 0) {
Session::put('cart', $cart);
} else {
Session::forget('cart');
}
return redirect()->back();
}
public function index()
{
$user = Auth()->user();
if (!Session::has('cart')) {
return view('frontend.cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
$total = $cart->totalPrice;
return view('frontend.cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice, 'user' => $user, 'total' => $total]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
这是我在刀片中的Options
代码:
<div class="options">
<h3>options:</h3>
<table class="table table-bordered table-striped">
<tbody>
@foreach($product->suboptions as $option)
<tr>
<th style="width:150px;">{{ $option->option->title }}</th>
<td class="text-left">
{{ Form::checkbox('option', '$option->id') }}{{$option->title}}-{{$option->price}}
</td>
</tr>
@endforeach
</tbody>
</table>
</div>