Level 73
You can use withCount to acheive that,
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I would like to know if it's possible to add an attribute to be part of a query. So what I mean is that in my Product model I have an attribute function that goes like this
public function getProductInfoAttribute()
{
return sprintf("%s > %s",
'['.$this->id.'] '.$this->name, $this->categories()->count());
}
and what I would like is that when I run a query
$products = Product::where('enabled', 1)->get();
my attribute will be part of that query.
Hope that made sense
Please or to participate in this conversation.