Laravel SELECT SUM 2列Eloquent

时间:2016-12-19 17:41:26

标签: php mysql laravel

SELECT 
     SUM(username = 'benjamin') as c1, 
     SUM(email = 'benjamin@hotmail.com') as c2 
FROM users WHERE active = 0

我有一个mysql查询需要检查2列匹配

但现在我需要把它写进Laravel Eloquent

任何人都知道如何在Eloquent中做到这一点?

1 个答案:

答案 0 :(得分:2)

你可以这样做:

User::where('active', 0)
    ->select(DB::raw("SUM(username = 'benjamin') as c1, SUM(email = 'benjamin@hotmail.com') as c2"))
    ->get();

希望这有帮助!