Eloquent eager load Order by

后端 未结 7 2145
花落未央
花落未央 2020-11-27 13:56

I have problem with eloquent query. I am using eager loading (one to one Relationship) to get \'student\' With the \'exam\', Using the code

7条回答
  •  忘掉有多难
    2020-11-27 14:42

    There is an alternative way of achieving the result you want to have without using joins. You can do the following to sort the students based on their exam's result. (Laravel 5.1):

    $students = Student::with('exam')->get();
    
    $students = $students->sortByDesc(function ($student, $key)
    {
        return $student->exam->result;
    });
    

提交回复
热议问题