Level 39
count() is an aggregate function and so MUST BE used with "GOUP BY"
https://www.sqltutorial.org/sql-aggregate-functions/
Add -> groupBy('templates.template_name', ...) before the ->get()
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Showing Error when taking count
$campaigns_user_history = DB::table('campaigns_history as camp_hist')
->join('campaigns', 'campaigns.id', '=', 'camp_hist.campaign_id')
->join('companies', 'companies.id', '=', 'campaigns.company_id')
->join('users', 'users.id', '=', 'camp_hist.user_id')
->join('templates', 'templates.id', '=', 'camp_hist.template_id')
->select('templates.template_name', 'companies.name as company_name', 'camp_hist.id as historyId','camp_hist.is_accept',
'camp_hist.campaign_id',
'camp_hist.updated_at',
'camp_hist.sent_total_count','users.name as username',
DB::raw('count(camp_hist.is_sent) as total_sent_count') )
->where('camp_hist.campaign_id', '=', $campaignId)
->get();
Error
SQLSTATE[42803]: Grouping error: 7 ERROR: column "templates.template_name" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: select "templates"."template_name",
Please or to participate in this conversation.