Php (1/1)此集合实例上不存在异常属性[price]。(拉威尔5.4)

Php (1/1)此集合实例上不存在异常属性[price]。(拉威尔5.4),php,laravel,shopping-cart,Php,Laravel,Shopping 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->it

我在购物车模型上有这个功能

//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'];
   }


}
我想知道价格,这样我就可以计算出来了。代码运行良好,但当我尝试在storedItem数组中添加'price'=>$item->price时。这是一个错误 (1/1)此集合实例上不存在异常属性[price]。(拉威尔5.4)

有人能帮我吗?我该怎么处理这个

这是我的刀片视图

@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>&nbsp;&nbsp;{{$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
@if(isset(Session::get('cart')->items))
@foreach(会话::get('cart')->项目作为$crt)
@foreach($crt['item']作为$pork)
{{$pork['pork_name']}
get()
即使只找到一个结果,也会返回一个集合,您可以使用
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>&nbsp;&nbsp;{{$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(会话::get('cart')->项目作为$crt)
{{$crt['item']->name}
get()
即使只找到一个结果,也会返回一个集合,您可以使用
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>&nbsp;&nbsp;{{$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(会话::get('cart')->项目作为$crt)
{{$crt['item']->name}

正如@wanghanlin所说的那样-您需要从
集合中选择
第一个
记录
-而且
会话::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'));

}

正如@wanghanlin所说的那样-您需要从
集合中选择
第一个
记录
-而且
会话::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(会话::get('cart')->项目作为$crt)
@foreach($crt['item']作为$pork)
{{$pork['pork_name']}
@if(isset(Session::get('cart')->items))
@foreach(会话::get('cart')->项目作为$crt)
@foreach($crt['item']作为$pork)
{{$pork['pork_name']}

我试了第一个,但没有显示猪肉的味道name@naning14你说没有显示猪肉的名字是什么意思?$pork是否使用first()获得pork模型的实例?我需要在刀片中显示pork的名称,但当我尝试使用first()而不是get()时,它没有显示其名称name@naning14把你的刀片文件也贴在这里,让我们看看有什么问题。实际上,我在获取猪肉及其数据方面没有问题。它工作得非常好。我的主要问题是:$storedItem=['qty'=>0,'item'=>$item,'price'=>$item->price];因此,当我尝试在该数组中添加'price->$item->price'时,它给出了一个错误(1/1)异常属性[price]在此集合实例中不存在。但是谢谢你,我学到了一些东西。我试了第一个,但是没有显示出猪肉的味道name@naning14你说没有显示猪肉的名字是什么意思?$pork是否使用first()获得pork模型的实例?我需要在刀片中显示pork的名称,但当我尝试使用first()而不是get()时,它没有显示其名称name@naning14把你的刀片文件也贴在这里,让我们看看有什么问题。实际上,我在获取猪肉及其数据方面没有问题。它工作得非常好。我的主要问题是:$storedItem=['qty'=>0,'item'=>$item,'price'=>$item->price];因此,当我尝试在该数组中添加'price->$item->price'时,它给出了一个错误(1/1)异常属性[price]在此集合实例中不存在。但是谢谢你,我学到了一些东西。它不会显示猪肉的名字。如果没有任何可用的记录,那么
first()
方法将返回
null
,因此我为该场景实例化了
new Port
,所以至少您有一个所需模型的实例。然后您应该做的是检查Pork是否确实存在
if($Pork->exists)
——这将告诉您是否至少有一条记录与
where
子句匹配。缺少
name
-我只能想象它与现有记录没有关联,或者您有一个空的
Pork
模型实例,因为没有与您的
where
子句匹配的记录。如果没有任何可用的记录,则它不会显示Pork的名称
方法将返回
null
,因此我为该场景实例化了
新端口
,这样至少您有一个所需模型的实例。然后您应该做的是检查Pork是否确实存在
if($Pork->exists)
——这将告诉您是否至少有一条记录与
where
子句匹配。缺少
名称
-我只能想象它不是与现有记录关联,就是您有一个空的
@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>&nbsp;&nbsp;{{$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