Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

satheeshkumarj's avatar

Not Able take count from Laravel Query builder

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",
0 likes
1 reply

Please or to participate in this conversation.