dd 可以看到数据,但是无法读取,是为什么

上代码:

$user_result = Auth::user()->topicanswers->where('topic_id',$this->id);
dd($user_result);

结果:

Illuminate\Database\Eloquent\Collection {#842 ▼
  #items: array:1 [0 => App\Models\TopicAnswer {#869 ▼
      #fillable: array:1 []
      #connection: "mysql"
      #table: "topic_answers"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:6 ["id" => 1
        "user_id" => 1
        "topic_id" => 2
        "result" => 2
        "created_at" => "2020-02-29 01:19:33"
        "updated_at" => "2020-02-29 01:19:35"
      ]
      #original: array:6 []
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 []
    }
  ]
}

继续,dd($user_result->result)报错:

Property [result] does not exist on this collection instance. (View: D:\Didongni\App\idongni\resources\views\mobile\topics\index.blade.php)

这是为什么,为什么读取不到2

好,再继续,dd(collect($user_result)),结果:

Illuminate\Support\Collection {#732 ▼
  #items: array:1 [0 => App\Models\TopicAnswer {#869 ▼
      #fillable: array:1 []
      #connection: "mysql"
      #table: "topic_answers"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:6 ["id" => 1
        "user_id" => 1
        "topic_id" => 2
        "result" => 2
        "created_at" => "2020-02-29 01:19:33"
        "updated_at" => "2020-02-29 01:19:35"
      ]
      #original: array:6 []
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 []
    }
  ]
}

这下总可以了吧,但是dd(collect($user_result)->result)依然报错:

Property [result] does not exist on this collection instance. (View: D:\Didongni\App\idongni\resources\views\mobile\topics\index.blade.php)

最后,$user_result = Auth::user()->topicanswers->where('topic_id',$this->id)[0]->result;,这样就可以,我记得不需要用下标啊,难道我迷糊了???

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
最佳答案
  • find()
  • first()

使用这2个方法可以直接访问属性获取相应的值,而你的代码实际用了 get() ,会返回一个数组。

试想一下,如果你返回了2条数据,你直接用 $user_result->result 应该显示哪条数据的 result 值呢?

4年前 评论
讨论数量: 5

就是字面意思,这个集合对象中没有 result 属性。应该这样来操作。
dd($user_result->first()->result) 或者 dd($user_result->items[0]->result)

你可以看到数据的结构,collection 中装的是 items ,items 是一个数组,数组的每一个元素中才有 result 这个属性。

4年前 评论
wongvio (楼主) 4年前
  • find()
  • first()

使用这2个方法可以直接访问属性获取相应的值,而你的代码实际用了 get() ,会返回一个数组。

试想一下,如果你返回了2条数据,你直接用 $user_result->result 应该显示哪条数据的 result 值呢?

4年前 评论

你没搞清楚,查询出来的是,单个模型,还是多个模型,,,

单个模型,结果是模型类的实例,当然可以直接获取模型的属性,

如果是多个模型的集合,结果是 Collection 类的实例,当然不能直接获取模型的属性,,,

4年前 评论
Epona

collection 是类似于数组的东西,当然需要下标了,你需要做的是 where('xxx','yyy')之后加一个

->first()

4年前 评论

非常感谢大家的解答

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!