【问题标题】:Laravel: cannot access item from a collectionLaravel:无法访问集合中的项目
【发布时间】:2018-09-17 21:12:42
【问题描述】:

我有一些数据作为关系的集合返回:

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

打印出来时,我可以看到我正在寻找的数据,但在尝试访问它时:

$item->detaileditem_id;

$item->detaileditem_name;

我收到一个错误:

例外:属性 [detaileditem_id] 在此不存在 集合实例。

【问题讨论】:

    标签: php laravel collections eloquent


    【解决方案1】:

    where 返回过滤后的集合,而不是单个项目。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-19
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多