orderBy on nested relation

Posted 1 year ago by saqueib

I am using a polymorphic relation and I want to get all the post order by comments.like_count but it's not working, I have tried following

Post::whereNotNull('published_at')
->with('comment')
->latest('comment.like_count')->paginate()

this gives error no column like_count from DB

Post::whereNotNull('published_at')
->with(['comment' => function($query) {
      $query->orderBy('like_count')
}])->paginate()

Post Model

class Post extends Model {

    public function comment()
    {
        return $this->morphTo();
    }

}

Please help

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

Reply to

Use Markdown with GitHub-flavored code blocks.