Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

t0berius's avatar

eloquent get result of query (true or false)

Any idea how I can easily query the database using the query below and save the status into a variable? Using something like whereHas() isn't the way to go I think.

$product = Product::find(3);
//just require a simple true or false
$productHasActiveSales = 
($this->whereHas('orders', function ($query) {
        $query->where('orders.created_at', '>=', Carbon::now()->subDays(3));
    })) ? true : false;
0 likes
1 reply
Nakov's avatar
Nakov
Best Answer
Level 73

You can do it like this I believe:

$productHasActiveSales = $product->whereHas('orders', function ($query) {
        $query->where('created_at', '>=', Carbon::now()->subDays(3));
    })->exists();

that will return true or false.

You don't need the orders. in your subquery.

1 like

Please or to participate in this conversation.