I am trying to execute the next SQL query:
SELECT sex, recuperated, count(case_id) as total_cases
FROM api.patients
GROUP BY recuperated, sex
ORDER BY total_cases DESC;
Which returns:
'41523', 'ANTIOQUIA', 'MEDELLIN'
'6066', 'ANTIOQUIA', 'BELLO'
'6056', 'AMAZONAS', 'LETICIA'
'3855', 'ANTIOQUIA', 'ITAGUI'
first in eloquent:
$query = Patient::query();
$statistics[0] = $query
->select("sex", "recuperated", "count (case_id) as total_cases")
->orderBy("total_cases","desc")
->groupBy("recuperated")
->get();
it returns:
Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'total_cases' in 'order clause' (SQL: select count(*) as aggregate from
patients
group byrecuperated
,sex
order bytotal_cases
desc)
second in eloquent:
$query = Patient::query();
$statistics[0] = $query
->select('sex','recuperated',DB::raw('count(case_id) AS total_cases'))
->orderBy('total_cases','DESC')
->groupBy('recuperated')
->get();
I am using MySql and laravel 7 and PHP 8