简体   繁体   English

(1/1)异常属性[price]在该集合实例上不存在。 (Laravel 5.4)

[英](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>&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

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>&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

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>&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

That's my blade view 那是我的刀锋

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该集合实例上不存在属性[title]。 拉拉韦尔5.5 - Property [title] does not exist on this collection instance. laravel 5.5 此集合实例上不存在属性 [expiry_date]。 在 Laravel - Property [expiry_date] does not exist on this collection instance. in Laravel 该集合实例上不存在属性[associated_occupation]。 -Laravel - Property [associated_occupation] does not exist on this collection instance. - Laravel 此集合实例上不存在属性 [vegan]。 Laravel - Property [vegan] does not exist on this collection instance. Laravel 此集合实例上不存在属性 [图像]。 / Laravel - with() 方法 - Property [image] does not exist on this collection instance. / Laravel - with() Method “此集合实例上不存在属性 [角色]。” - "Property [role] does not exist on this collection instance." 在laravel 5.4中发现错误,此集合实例上不存在属性[类别] - found error in laravel 5.4,Property [categories] does not exist on this collection instance Laravel错误:此集合实例上不存在属性[id]。 但是它可以在本地服务器上运行 - Laravel Error: Property [id] does not exist on this collection instance. Yet it works on the local server 此集合实例上不存在属性 [order_number]。 拉拉维尔 9.x - Property [order_number] does not exist on this collection instance. laravel 9.x “此集合实例上不存在属性[registration_types]。” - “Property [registration_types] does not exist on this collection instance.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM