Home » Archives for Francis Adu Gyamfi

Author Archives: Francis Adu Gyamfi

Laravel: Eloquent Sort By Relation

In Laravel 5.7, we have the Eloquent ORM to help with fetching data. However, sometimes we want to sort the data by a field in a related model and still retain the nested object structure of relations. public function getPrograms(Request $request, $id) { $limit = $request->limit ?? 15; $programs = SchedulePrograms::where('schedule_id', $id) ->with('program') ->orderBy('long_name', 'asc') // throws error ->paginate($limit); return ...

Read More »