Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Randy_Johnson's avatar

Collections, Array within an Array

Help, I would like the mothers and fathers collections to be like the students collection before passing it on.

array:4 [▼
  "teacher" => App\Models\Teacher {#1336 ▶}
  "students" => Illuminate\Database\Eloquent\Collection {#1126 ▼
    #items: array:2 [▼
      0 => App\Models\Student {#720 ▶}
      1 => App\Models\Student {#1335 ▶}
    ]
  }
  "mothers" => array:2 [▼
    0 => Illuminate\Support\Collection {#1348 ▼
      #items: array:1 [▼
        0 => {#1337 ▼
          +"name": "dfasdfsd"
          +"surname": "afdfasd"
          +"email": "dfasdfasdf"
          +"phone": "--fasfsdfas"
        }
      ]
    }
    1 => Illuminate\Support\Collection {#1352 ▼
      #items: []
    }
  ]
  "fathers" => array:2 [▼
    0 => Illuminate\Support\Collection {#1422 ▼
      #items: []
    }
    1 => Illuminate\Support\Collection {#1346 ▶}
  ]
]
            $father = DB::table('parents')
            ->join('student_parents', 'parents.id', '=', 'student_parents.parent_id')
            ->where('student_parents.student_id', $student->id)
            ->where('parents.mother', 0)
            ->select('parents.name', 'parents.surname', 'parents.email', 'parents.phone')
            ->get();

            $mother = DB::table('parents')
            ->join('student_parents', 'parents.id', '=', 'student_parents.parent_id')
            ->where('student_parents.student_id', $student->id)
            ->where('parents.mother', 1)
            ->select('parents.name', 'parents.surname', 'parents.email', 'parents.phone')
            ->get();
0 likes
2 replies
SilenceBringer's avatar

@randy_johnson use eloquent instead of DB facade to have eloquent collectio. Or wrap it

$fathers = new \Illuminate\Database\Eloquent\Collection($fathers->toArray())
1 like
Randy_Johnson's avatar
Randy_Johnson
OP
Best Answer
Level 8

Should I just read about collections, or can you share be a good tutorial on Youtube to get me started. I fixed it but it isn't clean.

    @for($i = 0; $i <= sizeof($data['students']) - 1; $i++)
        <tr>
            <td style="@if($i % 2 == 0) background-color: #F2F4F4; @else background-color: #FDFEFE; @endif">{{$data['students'][$i]->name ?? ""}}</td>
            <td style="@if($i % 2 == 0) background-color: #F2F4F4; @else background-color: #FDFEFE; @endif">{{$data['students'][$i]->surname ?? ""}}</td>
            <td style="@if($i % 2 == 0) background-color: #F2F4F4; @else background-color: #FDFEFE; @endif">{{$data['mothers'][$i][0]->name ?? ""}}</td>
            <td style="@if($i % 2 == 0) background-color: #F2F4F4; @else background-color: #FDFEFE; @endif">{{$data['mothers'][$i][0]->surname ?? ""}}</td>
            <td style="@if($i % 2 == 0) background-color: #F2F4F4; @else background-color: #FDFEFE; @endif">{{$data['mothers'][$i][0]->phone ?? ""}}</td>
            <td style="@if($i % 2 == 0) background-color: #F2F4F4; @else background-color: #FDFEFE; @endif">{{$data['mothers'][$i][0]->email ?? ""}}</td>
            <td style="@if($i % 2 == 0) background-color: #F2F4F4; @else background-color: #FDFEFE; @endif">{{$data['fathers'][$i][0]->name ?? ""}}</td>
            <td style="@if($i % 2 == 0) background-color: #F2F4F4; @else background-color: #FDFEFE; @endif">{{$data['fathers'][$i][0]->surname ?? ""}}</td>
            <td style="@if($i % 2 == 0) background-color: #F2F4F4; @else background-color: #FDFEFE; @endif">{{$data['fathers'][$i][0]->phone ?? ""}}</td>
            <td style="@if($i % 2 == 0) background-color: #F2F4F4; @else background-color: #FDFEFE; @endif">{{$data['fathers'][$i][0]->email ?? ""}}</td>
        </tr>
    @endfor

It's being pushed to an excel file. This is the reason for the funky style.

Please or to participate in this conversation.