mardi 11 octobre 2016

Laravel return records where children are under parents

I am retriving model like this people = Person::with('children')->get(); and this returns me dd()

Collection {#322 ▼
  #items: array:4 [▼
    0 => Person {#311 ▶}
      #relations: array:1 [▼
         "children" => Collection {#320 ▼
           #items: array:2 [▼
              0 => Child {#323 ▶}
              1 => Child {#324 ▶}
    1 => Person {#312 ▶}
    2 => Person {#313 ▶}
        #relations: array:1 [▼
         "children" => Collection {#320 ▼
           #items: array:2 [▼
              0 => Child {#323 ▶}
    3 => Person {#314 ▶}

But now i am trying to export this to excel but i needed to be like (children under their parent) for example:

Collection {#322 ▼
  #items: array:6 [▼
    0 => Person {#311 ▶} // Parent 1
    1 => Child {#312 ▶} // Child of parent 1
    2 => Child {#313 ▶} // Child of parent 1
    3 => Person {#314 ▶} // Parent 2 - Single (no relation)
    4 => Person {#315 ▶} // Parent 3 
    5 => Child {#316 ▶} // Child of parent 3
    6 => Person {#314 ▶} // Parent 4 - Single (no relation)

Im not sure on how to do this (Eloquent or Query Builder) ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire