Potti
8 months ago

where() returns other then matched records

Posted 8 months ago by Potti

I am trying return matched or lower prices records from DB. But there something off about the code.

let's say $price = 900000 and trying to return records like below.

->whereHas("plans", function($q) use ($price){
    $q->when($price, function($query, $price){
        return $query->where("plan_price", "<=", $price);                
    });
})

it returns but,

as you see in the image here: https://imgur.com/a/sWrHpMO

it's also return more then $price I thought where() method applies iteration inside plans relation. But it doesn't check the second plan's price.

and if I use collection like below.

->when($price, function($query) use ($price){
    $query->with(["plans" => function($query) use ($price){
            return $query->where("plan_price", "<=", $price);
        }]);
})

it's working. But this time, I start to return empty plans as well... I am pretty much stuck here.

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