L
L
lightsout932018-08-13 07:52:08
Laravel
lightsout93, 2018-08-13 07:52:08

How to compose a query, Eloquent Laravel?

there are 5 tables in the database (fields in brackets for each, respectively):
groups (id | title);
students(id | firstName | lastName | group_id);
subjects (id | title);
assessments(id | value | student_id | subjects_id);
group_subject(id | group_id | subject_id);
Connections:

class Group extends Model
{
    public function students()
    {
        return $this->belongsToMany(Student::class);
    }

    public function subjects()
    {
        return $this->belongsToMany(Subject::class);
    }
}

class Student extends Model
{
    protected $fillable = ['firstName', 'lastName'];

    public function group()
    {
        return $this->hasOne(Group::class);
    }

    public function assessments()
    {
        return $this->hasMany(Assessment::class);
    }

    public function subjects()
    {
        return $this->belongsToMany(Subject::class, 'assessments', 'student_id', 'subject_id');
    }
}

class Subject extends Model
{
    public function groups()
    {
        return $this->belongsToMany(Group::class);
    }

    public function assessments()
    {
        return $this->hasMany(Assessment::class);
    }

    public function students()
    {
        return $this->belongsToMany(Student::class);
    }
}

class Assessment extends Model
{
    public function subject()
    {
        return $this->belongsTo(Subject::class);
    }

    public function student()
    {
        return $this->belongsTo(Student::class);
    }
}

How can I correctly choose for each student those subjects that also correspond to his group in which he studies, so that there is no such problem that each student has different subjects, even if they are in the same group?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tolmachev, 2018-08-13
@dark_tke

Use Has many through

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question