您可以通过几种方法实现这一目标:
// when eager loading$school = School::with(['students' => function ($q) { $q->orderBy('whateverField', 'asc/desc');}])->find($schoolId);// when lazy loading$school = School::find($schoolId);$school->load(['students' => function ($q) { $q->orderBy('whateverField', 'asc/desc');}]);// or on the collection$school = School::find($schoolId);// asc$school->students->sortBy('whateverProperty');// desc$school->students->sortByDesc('whateverProperty');// or querying students directly$students = Student::whereHas('school', function ($q) use ($schoolId) { $q->where('id', $schoolId);})->orderBy('whateverField')->get();


