vendredi 27 janvier 2017

Laravel QueryBuilder - Select N rows for each foreign key

I'm trying to retrieve the latest 5 rows for each foreign key in a table, so imagine the following database structure:

CATEGORY (id, name)
ARTICLE (id, category_id, title)

I want do something like this:

Article::where("category_id", 1)
       -> latest()
       -> limit(5)
       -> get();

Article::where("category_id", 2)
       -> latest()
       -> limit(5)
       -> get();

And so on. How would I do this as a single query for all foreign keys?

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire