Laravel - 'ErrorException',消息'试图获取非对象的属性'

时间:2015-06-30 13:04:23

标签: php laravel

此应用程序可让您创建产品报价。但是当他们试图从购物车中删除某个商品时,他们会收到此错误:

  

[2015-06-29 20:58:28] production.ERROR:异常'ErrorException'   在/app/controllers/CustomQuoteController.php:613中显示消息'试图获取非对象的属性'

     

#0 /app/controllers/CustomQuoteController.php(613):Illuminate \ Exception \ Handler-> handleError(8,'试图获取p ...',   '/ var / www / ...',613,数组)

我删除了部分路径名称。

第613行是读取if($item_in_cart->name == $custom_quote_item->name)

的行
public function removeFromQuote()
{
    $item_exists = true;

    $custom_quote_item = CustomQuoteItem::find(Input::get('id'));

    $custom_quote_items = Session::get('custom_quote_items');

    if(count($custom_quote_items) > 0 ) 
    {
        foreach($custom_quote_items as $key => $item_in_cart)
        {
            // line 613 below
            if($item_in_cart->name == $custom_quote_item->name)
            {
                unset($custom_quote_items[$key]);

                Session::set('custom_quote_items', $custom_quote_items);

                return Redirect::back()->with('success', 'Item has been removed.');
            }
        }
    }

    return Redirect::back()->with('errors', 'Item was not removed.');
}

1 个答案:

答案 0 :(得分:1)

Since you're retrieving properties on both $item_in_cart and $custom_quote_item on line 613, that's where your problems lie. One of those isn't an object, or do not contain the property name, as the exception states.

I'd verify that both variables are objects by using dd($item_in_cart), and update us with the output, my guess is $item_in_cart is the issue, since you're retrieving it from the session and looping through it.