Laravel 5 查询生成器左连接

Laravel 5 Query Builder Left Join

我想在 laravel

中执行以下 mysql 命令
SELECT stars.*, SUM(points.amount) AS total_points
FROM stars
LEFT JOIN points ON stars.id = points.star_id 
GROUP BY stars.id

所以我写道:

\DB::table('stars')
            ->leftJoin('points', 'stars.id', '=', 'points.star_id')
            ->select(\DB::raw("stars.*, SUM('points.amount') AS total_points"))
            ->groupBY('stars.id')
            ->get();

当我转储并取消结果时,我看到 total_points 是 0.0。怎么了?

可能是引号的问题?

->select(\DB::raw("stars.*, SUM('points.amount') AS total_points"))

正确:

->select(\DB::raw("stars.*, SUM(points.amount) AS total_points"))

简单变体:

->selectRaw('stars.*, SUM(points.amount) AS total_points')