If you want to sum or count any specific column amount, using DB raw multiple when case example I have given below, or you wish to run a sub DB query with the multi where causes this example will help you how to sum or count.
using DB raw we will sum the total amount by applying multi "when" conditions in DB raw. below is an example of applying more than one condition in DB raw with the when case example I given juts follow and apply in your query for getting an expected result from the database.
use App\Models\SalesVerify;
use DB;
$response = SalesVerify::select(
DB::raw("sum(case when paid_by = 0 then sales_amount else 0 end) as total_payment_sum"),
DB::raw("sum(case when paid_by = 0 and verify = 0 then 1 else 0 end) as total_unverified_payment"),
DB::raw("sum(case when paid_by = 0 and verify = 1 then 1 else 0 end) as total_verified_payment"),
)
->whereDate('sales_date','>=',now()->subDays(30)->format('Y-m-d'))
->groupBy(DB::raw('Date(sales_date)'))
->orderBy('sales_date','desc')
->paginate();
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- average() Method | Laravel Collection Example
- Laravel 56 Login and Logout
- Import Export Database in JSON Format Laravel
- How to use Date Format Validation in Laravel?
- REST API Authentication With Laravel Passport - Webappfix
- could not find driver (SQL: select count(*) as aggregate from "users" where "email" = admin@gmail.com)
- Laravel OneSignal Push Notification Integrate
- Unique violation: 7 ERROR: duplicate key value violates unique constraint "users_pkey" DETAIL: Key (id)=(1) already exists.
- Create Custom Route File With New Service Provider Laravel 8 9
- Laravel Carbon diffForHumans Arguments Passing