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

Umbus's avatar
Level 1

@sinnbeck If you could I would appreciate it =)

@michaloravec I already tried that yesterday and today I did exactly as you said, it didn't work. Maybe because I'm testing this on localhost maybe

Sinnbeck's avatar

Something like this (not tested)

$trans = DB::table('posts')
                   ->select('project_id', DB::raw('SUM(amount) as transtotal'))
                   
                   ->groupBy('project_id');

$projects = DB::table('projects')
        ->joinSub($trans, 'total', function ($join) {
            $join->on('project.id', '=', 'total.project_id');
        })->get();
cookie_good's avatar

Don't hate me because I'm obvious. :P

$ans = DB::select("SELECT projects.* , SUM(transactions.amount) as total FROM projects LEFT JOIN transactions ON projects.id = transactions.project_id");

Add group by to raw sequel according to MySQL specs.

Previous

Please or to participate in this conversation.