Query with constraints and eager loading with constraints mixed together?

Posted 3 years ago by Axeia

Hello, I've a query which will need constraints on some data that's eagerly loaded. I've tried to achieve this with the following code:

        $pYears = explode('-', $searchQuery->input('year'));
        $pSizes = explode('-', $searchQuery->input('size'));

        $plushiesQ = Plushy::with('Colors', 'Photos')
            ->with(['Sizes' => function($query) use(&$pSizes){
                $query->whereBetween('Sizes.size', $pSizes);
            ->whereBetween('yearOfRelease', $pYears);
        echo $plushiesQ->toSql();
        $plushies = $plushiesQ->get()->toArray();

The year constraint gets applied successfully but the size constraint gets ignored. Is the only way to make this work by writing the join manually as most examples on the internet seem to do? The echo reflects that the size constraint gets ignored as well, it outputs select * from "Plushies" where "yearOfRelease" between ? and ?

PS: I did print_r out the$pSizes before inside the anonymous function, bit of an odd syntax not everyone might be familiar with but the innerpart does have access to the outside variable as expected.

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

Reply to

Use Markdown with GitHub-flavored code blocks.