Eloquent ORM: propose to add countDistinct expression #49281
-
This seems very trivial. Then, instead of doing this: // psalm warns about "string / implicit __toString mismatch" but it only works like this
$distinctCount = Model::query()->count(DB::raw('DISTINCT id')); I can do something like: $distinctCount = Model::query()->countDistinct('id'); Is this unavailable because not every DB query language supports the underlying |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Laravel sometimes does nothing in certain cases when a database engine doesn't support a certain operation. So this hardly qualifies as a rectiffication ^^ |
Beta Was this translation helpful? Give feedback.
-
Another interesting method would be |
Beta Was this translation helpful? Give feedback.
-
From the docs https://laravel.com/docs/10.x/database Laravel currently supports 5 databases:
A quick look online indicates that all the 5 databases can support The question then becomes whether there are specific reasons to not do it. EG maybe this violates the Eloquent design? |
Beta Was this translation helpful? Give feedback.
I revisited this some time later, and then I realized my smooth brain missed this very Eloquent expression that actually works:
Then, it actually boils down to "inadequate understanding of Laravel". And understandably, Taylor is uninterested to implement this.
Update: for more complicated queries e.g.
SELECT COUNT(*) FROM (SELECT GROUP BY HAVING)
, I can just doIt really is about the lack of understanding.
@shaedrich maybe something similar can be said for your
k, COUNT(*) ... GROUP BY k
case...? Maybe the correct (& clean) expression …