Skip to content

Instantly share code, notes, and snippets.

@grim-reapper
Forked from reinink/query.php
Created October 22, 2020 19:35
Show Gist options
  • Select an option

  • Save grim-reapper/2acd981d9003fbb272640fe4fc8abb17 to your computer and use it in GitHub Desktop.

Select an option

Save grim-reapper/2acd981d9003fbb272640fe4fc8abb17 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