【问题标题】:laravel get all models with relations and the relations of these relationslaravel 获取所有具有关系的模型以及这些关系的关系
【发布时间】:2017-06-03 00:58:19
【问题描述】:

块有两个关系

1- 一对多与区域

2-一对多带座位

而且地区和座位也有一对多的关系

3-区域也和座位是一对多的关系

这段代码

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

将返回 this ,它不发送区域关系

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

这里的地区有很多席位,这个代码也不会返回地区席位。 我只能获得街区区域和席位,而区域也有很多席位。

我应该先获得所有地区的 id,然后再尝试获得所有席位吗? ,有什么办法吗?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    使用nested eager loading:

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

    【讨论】:

      猜你喜欢
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 2021-08-22
      • 2021-02-09
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多