dton37910 2017-01-18 15:14
浏览 85
已采纳

laravel得到所有模型的关系和这些关系的关系

block has two relations

1- one to many with region

2- one to many with seat

and the region has one to many relation with seat too so

3- region has one to many relation with seat too

this code

 $block=Block::with('regions','seats')->where('id',$blockId)->get();

will return this , it doesn't send regions relations

0 => Block {#457 ▼
  #relations: array:2 [▼
    "regions" => Collection {#460 ▼
      #items: array:1 [▼
        0 => Region {#463 ▼
          #relations: []
        }
      ]
    }
    "seats" => Collection {#471 ▶}

here the region has many seats and this code will nor return region seats. i can just get the block regions and seats while regions has many seats too.

should i get all regions id first and then try to get it all seats ? , is there any way to this ?

  • 写回答

1条回答 默认 最新

  • doudun1029 2017-01-18 15:19
    关注

    Use nested eager loading:

    $block = Block::with('regions.seats', 'seats')->where('id', $blockId)->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊