Benjamin W
Benjamin W

Reputation: 2848

Laravel SELECT SUM 2 columns Eloquent

SELECT 
     SUM(username = 'benjamin') as c1, 
     SUM(email = '[email protected]') as c2 
FROM users WHERE active = 0

I have a mysql query need to check 2 columns match

But now I need to write it into Laravel Eloquent

anyone know how to do this in Eloquent?

Upvotes: 1

Views: 2387

Answers (1)

Saumya Rastogi
Saumya Rastogi

Reputation: 13709

You can do it like this:

User::where('active', 0)
    ->select(DB::raw("SUM(username = 'benjamin') as c1, SUM(email = '[email protected]') as c2"))
    ->get();

Hope this helps!

Upvotes: 2

Related Questions