Mohammed Riyadh
Mohammed Riyadh

Reputation: 1019

Laravel Select Count Where Query

I have Orders table and status column in my DB, the status is stored as [1,2,3,4] which is corresponding at frontend to [issued, pending, on thew ay, delivered] now I need to get the count of each status between 2 dates,

Please Note : I need the count of each status like,

issued : 80 pending : 50 on the way : 20 delivered : 170

I tried below code but don't no how to accomplish my needs

$account = DB::table('order')
                    ->whereBetween('created_at',[$fromdate, $todate])
                    ->select(DB::raw('COUNT(order.status) as total'))
                    ->get();
                    return response()->json($account,200);

Any help will be much appreciated

Upvotes: 0

Views: 806

Answers (2)

Sanjula Madumal Hewage
Sanjula Madumal Hewage

Reputation: 150

$account = DB::table('order')
    ->select(DB::raw('COUNT(order.id) as total'),'status')
    ->whereBetween('created_at',[$fromdate, $todate])
    ->whereIn('status',[1,2,3,4])
    ->groupBy('status')
    ->get();

return response()->json($account,200);

Upvotes: 2

gbalduzzi
gbalduzzi

Reputation: 10166

DB::table('order')
    ->select(DB::raw('COUNT(*) as total')
    ->whereBetween('created_at',[$fromdate, $todate])
    ->groupBy('status')
    ->get();

Upvotes: 0

Related Questions