How to sort by a field of the pivot table of a many-to-many relationship in Eloquent ORM

前端 未结 5 860
甜味超标
甜味超标 2020-12-14 15:22

I have been using Eloquent ORM for some time now and I know it quite well, but I can\'t do the following, while it\'s very easy to do in Fluent.

I h

5条回答
  •  孤城傲影
    2020-12-14 16:08

    Any method that's available in Fluent should also be available with Eloquent. Perhaps this is what you're looking for?

    $songs = Song->join('song_user', 'songs.id', '=', 'song_user.song_id')
    ->where('song_user.user_id', '=', $user->id)
    ->orderBy("song_user.play_count", "desc")
    ->get();
    

提交回复
热议问题