Home » Laravel Calculate multiple totals in conditional aggregates

Laravel Calculate multiple totals in conditional aggregates

Today, We want to share with you Laravel Calculate multiple totals in conditional aggregates.In this post we will show you wordpress plugin require another plugin, hear for Calculating totals in Laravel using conditional aggregates we will give you demo and example for implement.In this post, we will learn about How to Calculate Multiple Aggregate Functions in a Single Query using Laravel 6 with an example.

Laravel Calculate multiple totals in conditional aggregates

There are the Following The simple About how to combine two aggregate functions in Laravel Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel select multiple counts from one table, so the how to get count of multiple columns in Laravel is used for this example is following below.

Read Also:  Group By with Sum using Laravel Collection

Laravel use multiple where and sum Totals in single clause

Laravel conditional aggregates

$total = Visitor::count();
$tourism = Visitor::where('web_status', 'tourism')->count();
$acquaintances = Visitor::where('web_status', 'acquaintances')->count();
$conferences = Visitor::where('web_status', 'conferences')->count();
$relatives = Visitor::where('web_status', 'relatives')->count();

single database query

$visitors = Visitor::all();
$total = $visitors->count();
$tourism = $visitors->where('web_status', 'tourism')->count();
$acquaintances = $visitors->where('web_status', 'acquaintances')->count();
$conferences = $visitors->where('web_status', 'conferences')->count();
$relatives = $visitors->where('web_status', 'relatives')->count();

Laravel Calculate Conditional aggregates

MySQL Query

select
  count(*) as total,
  count(case when web_status = 'tourism' then 1 end) as tourism,
  count(case when web_status = 'acquaintances' then 1 end) as acquaintances,
  count(case when web_status = 'conferences' then 1 end) as conferences,
  count(case when web_status = 'relatives' then 1 end) as relatives
from visitors

Laravel 6 the query builder:

$totals = DB::table('visitors')
    ->selectRaw('count(*) as total')
    ->selectRaw("count(case when web_status = 'tourism' then 1 end) as tourism")
    ->selectRaw("count(case when web_status = 'acquaintances' then 1 end) as acquaintances")
    ->selectRaw("count(case when web_status = 'conferences' then 1 end) as conferences")
    ->selectRaw("count(case when web_status = 'relatives' then 1 end) as relatives")
    ->first();

<div>Total: {{ $totals->total }}</div>
<div>Tourism: {{ $totals->tourism }}</div>
<div>Acquaintances: {{ $totals->acquaintances }}</div>
<div>Conferences: {{ $totals->conferences }}</div>
<div>Relatives: {{ $totals->relatives }}</div>

Laravel Calculate Boolean columns

$totals = DB::table('visitors')
    ->selectRaw('count(*) as total')
    ->selectRaw('count(is_admin or null) as admins')
    ->selectRaw('count(is_treasurer or null) as treasurers')
    ->selectRaw('count(is_editor or null) as editors')
    ->selectRaw('count(is_manager or null) as managers')
    ->first();

Laravel Calculate Filter clauses

$totals = DB::table('visitors')
    ->selectRaw('count(*) as total')
    ->selectRaw('count(*) filter (where is_admin) as admins')
    ->selectRaw('count(*) filter (where is_treasurer) as treasurers')
    ->selectRaw('count(*) filter (where is_editor) as editors')
    ->selectRaw('count(*) filter (where is_manager) as managers')
    ->first();

Web Programming Tutorials Example with Demo

Read :

Read Also:  Get Selected Radio Button value using jQuery

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel multiple counts different tables.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Vue.js User Signup And Login Authentication Using PHP
  2. Read Also:  PHP CURL POST and GET REQUEST Methods
  3. Read Also:  Laravel Custom Dropdown Server Side Filter in Datatables
  4. Read Also:  crud operations in php
  5. Read Also:  Laravel 6 Delete File from public folder Example
  6. Read Also:  Laravel Selecting and Aliasing Columns in Eloquent's query
  7. Read Also:  VueJS Routing and Templating example
  8. Read Also:  Laravel Aggregate Object and Debugging eloquent methods
  9. Read Also:  vue.js check uncheck all checkboxes Example
  10. Read Also:  how to write subquery in sql

Leave a Comment

Your email address will not be published. Required fields are marked *