首页 > 解决方案 > 具有&符号未定义属性的Laravel集合

问题描述

我正在dd收集一个这样的集合,因为我在寻找对象上的属性时遇到问题:

dd($this->plan);

输出:

Illuminate\Support\Collection {#5314
  #items: array:3 [
    "ageRange" => "Under 18"
    "goal" => null
    "duration" => & "Indefinitely"
  ]
}

我正在寻找duration这个对象的属性,但是当我点击:$plan->duration我收到以下错误:

dd($this->plan->duration);

我收到以下错误:

Property [duration] does not exist on this collection instance.

我相信这与此有关&,但我不确定它为什么在那里或它来自哪里

标签: laravellaravel-collection

解决方案


That is an array, not an object inside the Collection. Try either $this->plan->get('duration') or $this->plan['duration']


As the question/answer in the comment by @Peppermintology points out, this is a because the 'duration' key was set by reference. To illustrate, here's a quick way to reproduce the behavior in the tinker console:

enter image description here


推荐阅读