简体   繁体   English

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

[英]Laravel: cannot access item from a collection

I've got some data returned as a collection from a relationship: 我从关系中以集合形式返回了一些数据:

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

when printing this out I can see the data I'm looking for but when trying to access it: 打印出来时,我可以看到我要查找的数据,但是当尝试访问它时:

$item->detaileditem_id;

or 要么

$item->detaileditem_name;

I'm getting an error: 我收到一个错误:

Exception: Property [detaileditem_id] does not exist on this collection instance. 例外:该集合实例上不存在属性[detaileditem_id]。

where returns a filtered collection, not a single item. 其中返回过滤的集合,而不是单个项目。

If you want the first item of that collection that matches your condition, use first: 如果您希望该集合中符合您条件的第一项,请首先使用:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM