我在CART模型上具有此功能
//Cart.php
<?php
namespace App;
class Cart
{
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 addCart($item, $id){
$storedItem=['qty'=>0, 'item'=>$item, 'price'=>$item->price];
if($this->items){
if(array_key_exists($id, $this->items)){
$storedItem= $this->items[$id];
}
}
$storedItem['qty']++;
$storedItem['price'] *= $storedItem['qty'];
$this->items[$id]= $storedItem;
$this->totalQty++;
$this->totalPrice +=$storedItem['price'];
}
}
这是我在控制器上的功能
public function addToCart(Request $request, $id){
$pork=Pork::where('did', $id)->get();
$oldCart=Session::has('cart') ? Session::get('cart') : null;
$cart= new Cart($oldCart);
$cart->addCart($pork, $id);
$request->session()->put('cart', $cart);
return redirect()->route('user.index', compact('pork'));
}
我正在尝试获取价格,以便可以计算出来。该代码运行良好,但是当我尝试添加"价格" => $ item->价格时。它给出错误是(1/1)此集合实例上不存在异常属性[价格]。(Laravel 5.4)
$storedItem=['qty'=>0, 'item'=>$item, 'price'=>$item->price];
有人可以帮我吗?我该怎么办?
这是我的刀片视图
@if(isset(Session::get('cart')->items))
@foreach(Session::get('cart')->items as $crt)
@foreach($crt['item'] as $pork)
<dl class="dl-horizontal">
<div id="cartdiv">
<dt>
<div>
<input type="number" min="1" value="{{$crt['qty']}}">
</dt>
<dd>
<label>
<b> {{$pork['pork_name']}}</b>
</label>
</dd>
<dt>
<label"> Price: <b id="bprice" name="price">{{$pork['basePrice']}}</b></label>
</dt>
<dd>
<label>Total Amount: 200.00</label>
</dd>
</dt>
</div>
</dl>
@endforeach
@endforeach
@endif
正如@wanghanlin所说的 - 您需要从collection
中选择first
记录 - Session::get()
也有第二个参数,默认情况下是null
,因此您可以重构为:
public function addToCart(Request $request, $id)
{
$pork = Pork::where('did', $id)->get()->first() ?? new Pork;
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
$cart->addCart($pork, $id);
$request->session()->put('cart', $cart);
return redirect()->route('user.index', compact('pork'));
}
get()
即使只找到1个结果,也可以使用 first()
获得第一个结果。
只是更改 $pork=Pork::where('did', $id)->get();
至$pork=Pork::where('did', $id)->first();
查看文件应更改为
@if(isset(Session::get('cart')->items))
@foreach(Session::get('cart')->items as $crt)
<dl class="dl-horizontal">
<div id="cartdiv">
<dt>
<div>
<input type="number" min="1" value="{{$crt['qty']}}">
</dt>
<dd>
<label>
<b> {{$crt['item']->name}}</b>
</label>
</dd>
<dt>
<label"> Price: <b id="bprice" name="price">{{$crt['item']->price}}</b></label>
</dt>
<dd>
<label>Total Amount: 200.00</label>
</dd>
</dt>
</div>
</dl>
@endforeach
@endif
@if(isset(Session::get('cart')->items))
@foreach(Session::get('cart')->items as $crt)
@foreach($crt['item'] as $pork)
<dl class="dl-horizontal">
<div id="cartdiv">
<dt>
<div>
<input type="number" min="1" value="{{$crt['qty']}}">
</dt>
<dd>
<label>
<b> {{$pork['pork_name']}}</b>
</label>
</dd>
<dt>
<label"> Price: <b id="bprice" name="price">{{$pork['basePrice']}}</b></label>
</dt>
<dd>
<label>Total Amount: 200.00</label>
</dd>
</dt>
</div>
</dl>
@endforeach
@endforeach
@endif
那是我的刀片视图