I have problem with eloquent query. I am using eager loading (one to one Relationship) to get \'student\' With the \'exam\', Using the code
If you ALWAYS want it sorted by exam result, you can add the sortBy call directly in the relationship function on the model.
public function exam() {
return this->hasMany(Exam::class)->orderBy('result');
}
(credit for this answer goes to pfriendly - he answered it here: How to sort an Eloquent subquery)