droxy80248 2017-11-13 15:44
浏览 41
已采纳

加载时自动访问Eloquent Relationship

I am working on a project which requires me to get all the list of all information from a table --Just like in a blog, i used the all() method to do this but when i try to get the method i declared in my Model i get an error, saying

the collection instance does not exists

But when i use The Model::find($id)->relationship()->name;

it works fine. Is there any way to load all relationship with the all() function in laravel.

Thanks for your help..

  • 写回答

2条回答 默认 最新

  • dongmu4591 2017-11-13 16:11
    关注

    When you perform Model::find($id)->relationship(); you are actually accesing to the Dynamic relationships Properties

    You need to convert it into a collection using Model::find($id)->relationship()->get();

    Then you can perform any collection method to get the result you want. After doing this you can access to its attributes like this:

    $model_varible = Model::find($id)->relationship()->get();
    $model_variable = $model_variable->find($id)->name;
    

    Let me know if this works for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)