There is nothing like this in Laravel, neither in the Query Builder nor in Eloquent, you have to do it a bit manually:
DB::table('comentarios')->select('post_id', DB::raw('COUNT(post_id) AS posts'))
->groupBy('post_id')
->orderBy('posts', 'DESC')
->limit(5)
->get();
However, there is little hope if you have relationships established with such a table, for example posts related to comments, which would allow us to count the number of comments in the relationship (assuming we want to count number of comments) and sort the count:
There is nothing like this in Laravel, neither in the Query Builder nor in Eloquent, you have to do it a bit manually:
However, there is little hope if you have relationships established with such a table, for example posts related to comments, which would allow us to count the number of comments in the relationship (assuming we want to count number of comments) and sort the count:
I put it this way by reviewing your answer:
but in the view it shows me the repeated elements, I create this line of code but it does not eliminate one of the repeated elements: