ralphmorris

Hi @bashy

Thanks for your reply.

Typically, an idea came to me just after posting this question and think I've got it working now!

I created a new relationship with the added thisMonth query scope.

    public function referralsThisMonth()
    {
        return $this->referrals()->thisMonth('referral_activated');
    }

And then modified the controller method as follows:

    public function getTopReferrersThisMonth()
    {
        return User::has('referralsThisMonth')
                    ->withCount('referralsThisMonth')
                    ->orderBy('referrals_this_month_count', 'desc')
                    ->take(3)
                    ->get();
    }

Return to Thread...