Eloquent eager load Order by

后端 未结 7 2149
花落未央
花落未央 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:55

    If you need to order your students collection by the result column, you will need to join the tables.

    Student::with('exam')
           ->join('exam', 'students.id', '=', 'exam.student_id')
           ->orderBy('exam.result', 'DESC')
           ->get()
    

    In this case, assuming you have a column student_id and your exams table are named exam.

提交回复
热议问题