Not sure if you plan to move to Laravel 4, but here’s an Eloquent example for sorting by a pivot tables fields:
public function songs() {
return $this
->belongsToMany('Song')
->withPivot('play_count')
->orderBy('pivot_play_count', 'desc');
}
withPivot
is like the eloquent with
and will add the play_count
field from the pivot table to the other keys it already includes. All the pivot table fields are prefixed with pivot
in the results, so you can reference them directly in the orderBy
.
I’ve no idea what it would look like in Laravel 3, but perhaps this will help point you in the right direction.
Cheers!