stats = Payment::groupBy('status','payment_type') ->selectRaw('status, payment_type, COUNT(id) as count, SUM(amount) as total_amount') ->get(); } public function getRecordsStats($status){ return $this->stats->where('status',$status) ->sum('count') ?? 0; } public function getPaymentStats($type) { return $this->stats->where('payment_type',$type) ->sum('count') ?? 0; } public function getAmountStats($type){ return $this->stats->where('payment_type',$type) ->where('status','approved') ->sum('total_amount') ?? 0; } }