Posted: 02-05-2017

Eloquent Tips - Orderby relationship

Eloquent Tips - Orderby relationship

The best way I've found to order a model by a relation field:

$countries = Countries::select('cities.*,countries.name as country')
    ->join('countries', 'countries.id', '=', 'cities.country_id')
    ->orderBy('countries.name', 'desc')
    ->paginate(10);