Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

n-other's avatar

Nested Relationships Query

Hello,

I'm having hard time to build proper eloquent query for the following models chain and their relationships:

very short table representation:

users (user_id);

apps (id, user_id);

fees (id, app_id);

purchases (id, fee_id, payment_id);

payments (id);

refunds (id, payment_id);

relations:

users.id 1-M apps.user_id;

apps.id 1-M fees.app_id;

fees.id 1-M purchases.fee_id;

purchases.payment_id 1-1 payments.id;

payments.id 1-1 refunds.payment_id

Proper relationships are set on all Models.

I need to query all refunds by using eloquent relations where user_id = 1. How do I do that?

So far, I was able to build following 1st level query:

      $p = \App\Models\Refunds::with('payment')->whereHas('payment', function($query) {
          $query->where('payments.id' => '70');
      })->get()->toArray();

However, I don't understand how do I go deeper via relations properly.

0 likes
7 replies
Vilfago's avatar

It's sometimes easier to start with the User if it's what you need,

$user = User::with('apps.fees.purchases.payments.refunds')->findOrFail(1);
Snapey's avatar

to be honest, this type of requirement is so common, I would probably put a user_id on more models. Definately payments, purchases and fees.

n-other's avatar

@VILFAGO - Unfortunately, this approach will create subsequent queries of all corresponding models starting from Users. I need a way to create chain of left joins. Of course I could write an SQL, but this is not the right way :(

n-other's avatar

@SNAPEY - This is more like a hack, than a solution. I used to think about this approach, however, my mother always taught me to make normalized databases :)

Cronix's avatar

Having additional id's to make querying data faster/more efficient for differing scenarios isn't a violation though. Do you think your bank is running through every credit/debit in your accounts history to give you your current balance? That would be the most strict definition of a normalized database and not storing repeated data (or data that you could otherwise calculate), but querying a separate balance table that keeps a running tally is more efficient, and by orders of magnitude.

Snapey's avatar

I'm only suggesting you have a foreign key to the user model, not put the whole user in there.

Just because you can form a chain through 5 different tables to reach the answer you want does not mean you should.

But, anyway, you don't need to write raw queries, you can still use QB joins and select to get the data you want

n-other's avatar

@CRONIX - Heh,

You didn't get my point. If there is a relation it should work as a relation both ways.

As far as I understand after a bit digging, laravel relations does not provide complete functionality in case left join/inner join is required with multiple tables.

I've found the package which would help me: https://github.com/fico7489/laravel-eloquent-join

Please or to participate in this conversation.