【问题标题】:(1/1) Exception Property [price] does not exist on this collection instance. (Laravel 5.4)(1/1) 此集合实例上不存在异常属性 [价格]。 (Laravel 5.4)
【发布时间】:2017-09-27 14:32:48
【问题描述】:

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

//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'));

}

我正在尝试获取价格,以便进行计算。代码运行良好,但是当我尝试在 storedItem 数组上添加 'price'=>$item->price 时。它给出的错误是 (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>&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

【问题讨论】:

    标签: php laravel shopping-cart


    【解决方案1】:

    正如@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'));
    
    }
    

    【讨论】:

    • 它也没有显示猪肉的名字
    • 如果没有可用的记录,那么first() 方法将返回null,因此我已经为该场景实例化了new Port,所以至少你有一个所需模型的实例。然后你应该做的是检查 Pork 是否真的存在 if ($pork-&gt;exists) - 这将告诉你是否找到了至少一条匹配 where 子句的记录。缺少name - 我只能想象它与现有记录无关,或者您有一个空的Pork 模型实例,因为没有与您的where 子句匹配的记录。
    【解决方案2】:

    get() 将返回一个集合,即使只找到 1 个结果,您可以使用 first() 获取第一个结果。

    只是改变 $pork=Pork::where('did', $id)-&gt;get();$pork=Pork::where('did', $id)-&gt;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
    

    【讨论】:

    • 我尝试了第一个()但它没有显示猪肉的名字
    • @naning14 没有显示猪肉的名称是什么意思? $pork 是否使用 first() 获得了 Pork 模型的实例?
    • 我需要在刀片中显示猪肉的名称,但是当我尝试使用 first() 而不是 get() 时,它没有显示其名称
    • @naning14 把你的blade文件也贴在这里,看看有什么问题
    • 我实际上在获取 Pork 及其数据方面没有问题。它真的很好用。我的主要问题在这里: $storedItem=['qty'=>0, 'item'=>$item, 'price'=>$item->price];所以当我尝试在该数组上添加 'price->$item->price 时,它​​给了我这个错误(1/1)异常属性 [price] 在这个集合实例上不存在。不过谢谢你,我学到了一些东西。
    【解决方案3】:
    @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
    

    这是我的刀片视图

    【讨论】:

    • 你可以试试$pork->pork_name 代替$pork['pork_name'],价格一样
    猜你喜欢
    • 2020-09-25
    • 2019-06-21
    • 2020-07-11
    • 2019-07-10
    • 2021-11-19
    • 2021-07-23
    • 2021-09-07
    • 2019-04-08
    相关资源
    最近更新 更多