Laravel Calculate multiple totals in conditional aggregates

Laravel Redirect To Another URL / Web Page - HTTP Laravel Redirects

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.

Read Also:  Laravel 5.8 Global Blade view variable

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.

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:  Group By with Sum using Laravel Collection

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.

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

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