9 months ago

Nested Relationships Query

Posted 9 months ago by n-other


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);


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');

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

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