Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉雷维尔';错误异常';带有消息';正在尝试获取非对象的属性';_Php_Laravel - Fatal编程技术网

Php 拉雷维尔';错误异常';带有消息';正在尝试获取非对象的属性';

Php 拉雷维尔';错误异常';带有消息';正在尝试获取非对象的属性';,php,laravel,Php,Laravel,此应用程序允许您创建产品报价。但是,当他们试图从购物车中删除项目时,会出现以下错误: [2015-06-29 20:58:28]生产。错误:异常“ErrorException” 在/app/controllers/CustomQuoteControl.php:613中显示消息“正在尝试获取非对象的属性” #0/app/controllers/CustomQuoteController.php(613):illumb\Exception\Handler->handleError(8,“尝试获取p…

此应用程序允许您创建产品报价。但是,当他们试图从购物车中删除项目时,会出现以下错误:

[2015-06-29 20:58:28]生产。错误:异常“ErrorException” 在/app/controllers/CustomQuoteControl.php:613中显示消息“正在尝试获取非对象的属性”

#0/app/controllers/CustomQuoteController.php(613):illumb\Exception\Handler->handleError(8,“尝试获取p…”, “/var/www/…”,613,数组)

我删除了部分路径名

第613行显示的是
if($item\u in\u cart->name==$custom\u quote\u item->name)


由于您正在第613行检索购物车中的
$item_
$custom\u quote\u item
上的属性,这就是您的问题所在。其中一个不是对象,或者不包含属性
name
,如异常状态所示


我会通过使用
dd($item\u in\u cart)
来验证这两个变量是否都是对象,并用输出更新我们,我猜是
$item\u in\u cart
是问题所在,因为您正在从会话检索它并在其中循环。

什么类型是
$custom\u quote\u items
?我猜这是一个\illumb\Support\Collection而不是数组,所以
如果(count($custom\u quote\u items)>0)
不起作用,应该是
如果(!$custom\u quote\u items->isEmpty())
。@Quasdunk,这是不正确的。Laravel的集合支持数组访问,使用
count($collection)
就可以了。还有一个
$collection->count()
方法可用于集合,他也可以使用。@SteveBauman是的,你说得对!但我记得在L4.0中遇到了一个问题,我花了相当长的时间发现集合计数不起作用。但要么我记不清了,要么它在后来的版本中被修复了:)无论如何,谢谢你指出它!这是一个数组。我继承了这方面的支持,因此了解他们做了什么,并找出问题所在。在这个控制器中发布整个代码会有帮助吗?那么,
$item\u in\u cart
或者
$custom\u quote\u item
null
。你能发布
dd($custom\u quote\u items,$custom\u quote\u items)的输出吗?并确保
CustomQuoteItem::find(输入::获取('id'))返回一些内容。您可能需要执行
CustomQuoteItem::findOrFail(…)对象(CustomQuoteItem)[99]受保护的“受保护的”=>数组(大小=0)受空保护的“表”=>字符串“自定义项”(长度=18)公共“价格”=>字符串“$0.60”(长度=5)受保护的“连接”=>受空保护的“主密钥”=>字符串“id”(长度=2)受保护的“perPage”=>int15 public“incrementing”=>boolean-true-public“timestamps”=>boolean-true-protected“attributes”=>array(size=3)“name”=>string“10255.011010”(length=12)“description”=>string“10 x 10 x 1”(length=11)“数量”=>string“12.000”(length=6)我做了一个dd($custom quote\u项目);它是空的。所以现在认为它一定是顶部的查找部分。
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.');
}