Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent

I have a Posts DB and a likes DB with the following structure:

Posts Table: item_id
Likes Table: item_id, like_id

How can I sort my posts by their amount of likes without adding a "likes" field to my Posts table?

Last updated 2 years ago.
0

The most efficient way would be database query with JOIN, GROUP BY and COUNT. Easier way is something like this: http://stackoverflow.com/questions/24208502/laravel-orderby-relationship-count

0

/\ - Good article.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

amilajack amilajack Joined 19 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.