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

BeggarSo's avatar

filtering relationships

I need to get a collection of all the students in all the sessions in all timeslot on each day...

Student model:

    public function sessions()
    {
        return $this->belongsToMany('App\Session')->withTimestamps();
    }

Session model:

public function students()
    {
        return $this->belongsToMany('App\Student')->withTimestamps();
    }

    public function timeslot()
    {
        return $this->belongsTo('App\Timeslot');
    }

Timeslot model

    public function sessions()
    {
        return $this->hasMany('App\Session');
    }

Timeslot table has columns "day" and "slot".

If i wanted to get a large collection containing the days => slots =>sessions => students should i just iterate over days then timeslots then sessions and students and try to add to a collection or is there a better way (I hope so)...

0 likes
0 replies

Please or to participate in this conversation.