ehabafia
1 month ago

Where in SubQuery

Posted 1 month ago by ehabafia

Hello Laracasts,

I am creating a subQuery and trying to put a condition in the subQuery itself but it's giving me an error. Here is an example:

        $newBorrowers = Borrower::newBorrowers()->get();
    public function scopeNewBorrowers($query)
    {
        return $query->where('login_id', '!=', null)->addSelect([
                'finance_count' => function ($q) {
                    $q->from('finances')
                        ->select(\DB::raw('count(id)'))
                        ->whereColumn('borrowers.id', 'finances.borrower_id');
                }
            ]);
    }

The condition I am trying to have is I want to get the newBorrowers who got finance_count = 0;

So, any borrower that doesn't have any finances is what I need to get.

Is there a good, or better what than what I am doing here?

Thank you,

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