Let's say we have a Post model and a Comment model. The Comment model has a belongsTo()
relationship with the Post, while the Post has a hasMany()
relationship with the comment. I am using the code Post::orderBy('comments_count')->get();
but not working.
in the model you would have:
public function scopecountDescending($query)
{
return $query->orderBy('comments_count','DESC');
}
And in your controller:
$posts = Post::countDescending()->get();
or simply in your controller:
Post::orderBy('comments_count' , 'DESC')->get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.