Skip to content

Instantly share code, notes, and snippets.

@reinink
Created May 20, 2019 12:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save reinink/e3fa25eae14aab7d3301e4be5510f354 to your computer and use it in GitHub Desktop.
Save reinink/e3fa25eae14aab7d3301e4be5510f354 to your computer and use it in GitHub Desktop.
Getting table totals using Laravel Eloquent
<?php
$totals = DB::table('subscribers')
->addSelect(DB::raw('count(*) as all'))
->addSelect(DB::raw("sum(case when status = 'confirmed' then 1 else 0 end) as confirmed"))
->addSelect(DB::raw("sum(case when status = 'unconfirmed' then 1 else 0 end) as unconfirmed"))
->addSelect(DB::raw("sum(case when status = 'cancelled' then 1 else 0 end) as cancelled"))
->addSelect(DB::raw("sum(case when status = 'bounced' then 1 else 0 end) as bounced"))
->addSelect(DB::raw("sum(case when status = 'inactive' then 1 else 0 end) as inactive"))
->get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment