Answer the question
In order to leave comments, you need to log in
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);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question