Skip to content

Instantly share code, notes, and snippets.

@sander3
Last active December 8, 2020 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sander3/76f50787baff97222eaa35ee263a7428 to your computer and use it in GitHub Desktop.
Save sander3/76f50787baff97222eaa35ee263a7428 to your computer and use it in GitHub Desktop.
Medium post 1, Gist 2
$teams = App\Team::with([
    'portfolios' => function ($query) use ($user) {
        // Only include portfolios where...
        $query->whereHas('wallets', function ($query) use ($user) {
            // ...there are wallets...
            $query->whereHas('transactions', function ($query) {
                // ...with pending transactions...
                $query->whereStatus('pending');
            });

            // ...to be received by the given user.
            $query->where('walletable_id', $user->id);
            $query->where('walletable_type', App\User::class);
        });
    },
    'portfolios.wallets' => function ($query) use ($user) {
        // Second layer of constraints
        $query->whereHas('transactions', function ($query) {
            $query->whereStatus('pending');
        });

        $query->where('walletable_id', $user->id);
        $query->where('walletable_type', App\User::class);
    },
    'portfolios.wallets.transactions' => function ($query) {
        // Third and final layer of constraints
        $query->whereStatus('pending');
    },
])->get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment