5

I have a subscription platform with Laravel Cashier and Stripe. I am trying to create an admin backend where the Admin can see all subscription payments made and also the total amount the site has generated in subscriptions. I was able to roughly get a list of payments (through the invoices) as follows:

// create an empty collection to hold the invoices
$users = User::whereNotNull('stripe_id')->get();
foreach($users as $user){
    $user->invoices = $user->invoices();
}

this adds all user's invoices to each user, and I can then loop through them in my view to get the list of all invoices and amount paid.

Now the issue is, when I try to get the total subscription payments for all invoices, the sum returns zero. This is how I try to get the total payments:

$total_payments = $users->sum('invoices.total')    

This returns 0 always. I have also tried to re-write this like.

$total_payments = $user->sum(function ($inv){
    return $inv->invoices->sum('total')
}

and this also returns 0. Not sure of a better way to do this. I'm using L5.4.

Alex
  • 193
  • 1
  • 2
  • 12

0 Answers0