Using COUNT() function in Eloquent ORM (outside Laravel)
问题 I'm trying to replicate an SQL query with Eloquent ORM: SELECT DISTINCT name, COUNT(name) AS total FROM tags WHERE question_id IS NOT NULL GROUP BY name ORDER BY name ASC; Here is my attempt in Eloquent ORM: $query = Tag::query(); $query->orderBy('name', 'asc'); ->groupBy('name'); ->select( array('count(name) as total') ); ->whereNotNull('question_id'); However, this gives me the following error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count(name)' in 'field list' (SQL: SELECT