dongqiaozhe5070 2014-10-23 20:08
浏览 94
已采纳

Laravel在数组Error上调用成员函数toArray()

I was hoping someone could possibly clear up a little confusion I've been having with this Error. So here is my code. (note) the User model has a hasMany relationship to Image

    $user = User::with('profile')->whereUsername($username)->firstOrFail();

    $images = $user->images->all();

    dd($user->toArray());

My confusion is dd($user->toArray()); works perfectly fine. But when I try this dd($images->toArray()) I get a Call to a member function toArray() on array. This has been killing me for a while. $user and $images are both objects but toArray() only works on $user. Also, $images[0]->toArray() works fine too so that just adds to the confusion.

  • 写回答

1条回答 默认 最新

  • dongshiliao7990 2014-10-23 20:19
    关注

    I think this happens because $images is a collection of objects not an object like $user. Check querying-relations in the documentation the red area in the end of that section. That explains why $images[0]->toArray() works fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?