$posts = Post::with('comments')
->join('comments', 'comments.post_id', '=', 'posts.id')
->select('posts.*', 'comments.*')
->groupBy('posts.id')
->orderBy(DB::raw('COUNT(`posts`.`id`)'), 'asc')
->get();
$postsWithCountOfComments = Post::withCount("comments")->get();
$sortedPosts = $postsWithCountOfComments->sortByDesc('comments_count');