Php Laravel:无法访问集合中的项

Php Laravel:无法访问集合中的项,php,laravel,collections,eloquent,Php,Laravel,Collections,Eloquent,我从关系中获得了一些作为集合返回的数据: $item = $this->detailedItems->where('detaileditem_product_id', $productId); 打印时,我可以看到我正在查找的数据,但在尝试访问时: $item->detaileditem_id; 或 我得到一个错误: 异常:此服务器上不存在属性[detaileditem\u id] 集合实例 返回已筛选的集合,而不是单个项 如果希望该集合中的第一项符合您的条件,请首先使用:

我从关系中获得了一些作为集合返回的数据:

$item = $this->detailedItems->where('detaileditem_product_id', $productId);
打印时,我可以看到我正在查找的数据,但在尝试访问时:

$item->detaileditem_id;

我得到一个错误:

异常:此服务器上不存在属性[detaileditem\u id] 集合实例

返回已筛选的集合,而不是单个项

如果希望该集合中的第一项符合您的条件,请首先使用:

$item = $this->detailedItems->where('detaileditem_product_id', $productId)->first();
$item = $this->detailedItems->where('detaileditem_product_id', $productId)->first();