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

JoaoHamerski's avatar

Is it possible using model methods to make a query?

For example, i have a method in my model to check if the Product is paid.

public function isPaid()
{
    return $this->getRemainingValue() <= 0 && ($this->value != null);
}

How can i iterate over all registers and get only the models that "is paid" but using this method, without writing a new query. Something like iterate over all registers and return all models that are true using isPaid method.

0 likes
5 replies
JoaoHamerski's avatar

I did this:

foreach(Product::all() as $product) {
   if ($product->isPaid()) {
        $products[] = $product;
   }
}

But idk if is the best way, there is another way to do that?

JoaoHamerski's avatar

@jlrdw i did, but query scopes only uses query variable, and i need to use a model variable to call a method in my model that checks something.

With queries i cant do $query->methodInMyModel().

JoaoHamerski's avatar

Look at my reply, is it the correct way to do it maybe, or there is a better way with query scopes?

Please or to participate in this conversation.