Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel get all models with relations and the relations of these relations

Tags:

php

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 ?

like image 307
Afshin Izadi Avatar asked Oct 18 '22 18:10

Afshin Izadi


1 Answers

Use nested eager loading:

$block = Block::with('regions.seats', 'seats')->where('id', $blockId)->get();
like image 131
Alexey Mezenin Avatar answered Nov 02 '22 09:11

Alexey Mezenin