Basic Query - Order By Count

Posted 3 years ago by itstrueimryan

Not sure what I'm doing wrong, but this isn't producing the right results. The query I want to replicate is simple - no relationships or anything. Just trying to get the tag_ids from tag_topic with the highest number of occurrences:

SELECT tag_id FROM `tag_topic` GROUP BY tag_id ORDER BY COUNT(tag_id) DESC LIMIT 8

And here is my Eloquent code:

$tagIds = \DB::table('tag_topic')
            ->orderBy(\DB::raw('count(tag_id)', 'DESC'))

And I just get some really wacky results, clearly not the right ones.

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.