i am trying change the this eloquent builder
$students = DiaryFrequency::select('students.name as student_name',
'students.id as student_id',
'diaries.id as diary_id',
'diary_frequency.presence as presence',
'classrooms.id as classroom_id',
'classrooms.code as classroom_code'
)
->join("students", "students.id", "=", "diary_frequency.student_id")
->join("diaries", "diaries.id", "=", "diary_frequency.diary_id")
->join("classrooms", "diaries.classroom_id", "=", "classrooms.id")
->where([
"diaries.classroom_id"=> $fields["classroom_id"],
"diaries.date"=> $fields["date"],
])
->distinct("distinct students.name")
->orderBy("students.name")
->get();
to this eloquent query
$students = Student::select('id','name')
->whereHas("diaries", function($query) use ($fields){
$query->where([
"classroom_id"=> $fields["classroom_id"],
"date"=> $fields["date"]
]);
})
->with("diaryFrequency", function($query) {
$query->select('student_id','presence');
})
->orderBy("name")
->get();
and i have a problem with it:
The 'presence's' is outing to all classrooms and i need that it out only to $fields["classroom_id"].
The relation:
class Student extends Model {
....
public function diaryFrequency() : HasMany {
return $this->hasMany(DiaryFrequency::class);
}
The Model DiaryFrequency has a relation with diary:
class DiaryFrequency extends Model {
...
public function diaries () : BelongsTo {
return $this->belongsTo(Diary::class, 'diary_id');
}
What i do to exlude the relations that dont be of $fields["classroom_id"]?