[英](1/1) Exception Property [price] does not exist on this collection instance. (Laravel 5.4)
I have this function on Cart Model 我在购物车模型上具有此功能
//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'];
}
}
and this is my function on my controller 这是我在控制器上的功能
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'));
}
I am trying to get the price so I could calculate it. 我试图获取价格,以便可以进行计算。 The code is working well, but when i tried adding 'price'=>$item->price on the storedItem array.
代码运行良好,但是当我尝试在storedItem数组上添加'price'=> $ item-> price时。 It is giving error which is (1/1) Exception Property [price] does not exist on this collection instance.
它给出的错误是(1/1)此集合实例上不存在异常属性[price]。 (Laravel 5.4)
(Laravel 5.4)
$storedItem=['qty'=>0, 'item'=>$item, 'price'=>$item->price];
could anyone help me? 谁能帮我吗? what should i do with this?
我该怎么办?
here is my blade view 这是我的刀片视图
@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
get()
will return a collection even if only 1 result found, you can use first()
to get the first result. 即使仅找到1个结果,
get()
也会返回一个集合,您可以使用first()
获得第一个结果。
Just change $pork=Pork::where('did', $id)->get();
只需更改
$pork=Pork::where('did', $id)->get();
to $pork=Pork::where('did', $id)->first();
到
$pork=Pork::where('did', $id)->first();
view file should changed to 查看文件应更改为
@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
As @wanghanlin has stated - you need to pick the first
record from the collection
- also Session::get()
has second argument, which by default is null
so you could refactor as: 正如@wanghanlin所说的-您需要从
collection
选择first
记录-Session 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'));
}
@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
That's my blade view 那是我的刀锋
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.