orderBy laravel belongsTo code example
Example: laravel belongs to order by
$school = School::with(['students' => function ($q) {
$q->orderBy('whateverField', 'asc/desc');
}])->find($schoolId);
$school = School::find($schoolId);
$school->load(['students' => function ($q) {
$q->orderBy('whateverField', 'asc/desc');
}]);
$school = School::find($schoolId);
$school->students->sortBy('whateverProperty');
$school->students->sortByDesc('whateverProperty');
$students = Student::whereHas('school', function ($q) use ($schoolId) {
$q->where('id', $schoolId);
})->orderBy('whateverField')->get();