saimiris
3 years ago

Sorting by associated model criteria

Posted 3 years ago by saimiris

Hi there,

I cannot find a way to apply the orderBy method to a query, using an associated model criteria. Basically I would like to be able to write something like : ->orderBy('user.email', 'asc'); but this does not work. What would be the correct syntax to do so then?

== CONTROLLER ==

$partners = Partner::where('partners.id', '>', 1) ->whereHas('user', function ( $query ) { $query->where('active', 1); });

== MODEL ==

class Partner extends Model { public function user() { return $this->belongsTo('App\User'); } }

Please sign in or create an account to participate in this conversation.