Support the ongoing development of Laravel.io →
Eloquent

I know that I can run this query and get the results that I want but I'm still learning Laravel and would like to do this using Eloquent instead of raw SQL. I've tried a number of ways to get this to work but I'm having no luck. I'm trying to model the following query:

SELECT collections.*, collection_token.*, games.*, sets.*, tokens.* FROM communal_haven.collections
INNER JOIN collection_token ON collection_token.collection_id = collections.id
INNER JOIN tokens ON tokens.id = collection_token.token_id
INNER JOIN sets ON sets.id = tokens.set_id
INNER JOIN games ON games.id = collections.game_id AND games.status != 'denied'
ORDER BY collections.user_id, collections.game_id

It's pretty straight forward with the joins on the different table IDs with the only qualifier being the games.status column. On each of the models (collections, collection_token, games, sets, tokens), I do have the various relationship methods and they work just fine. However, all of that gets pulled in using lazy loading and given the amount of information I need, it creates a lot of overhead when a single query like the above can get the data I need/want in one go.

Is there something I can use to set up that all those inner joins? I couldn't get it to work...

N.b., the above query is pulling in more information (via the .*) than I actually need. I just had it in there so I could verify the data as I was building it out.

thnx,
Christoph

Last updated by @christophboget 2 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

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.