Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

illmatic's avatar

Getting Invoice/Receipt of selected Subscription Plan

Hi,

I've implemented cashier for 2 types of subscriptions. Each separate "products" in Stripe. I'm trying to generate a receipt using the downloadInvoice function.

To get the ID of the invoice, I want to be sure I query for the selected subscription. I think I have the flow but getting stuck querying the nested Invoice attributes in the collection.

// Get subscription by product/plan name
$subscription = $model->subscription($model->plan_name);

// Get invoice based on subscription id
dd($model->invoices()->where('invoice.subscription','=',$subscription->stripe_id));

That comes out null even though if I dd on $model->invoices() I see the invoice in the collection with the subscription attribute.

Collection {#571 ▼
  #items: array:1 [▼
    0 => Invoice {#539 ▼
      #owner: Business {#574 ▶}
      #invoice: Invoice {#568 ▶}
      #items: null
    }
  ]
}

Am I complicating this or am I querying incorrectly?

0 likes
3 replies
Sti3bas's avatar

@illmatic invoice is a protected property. Luckily,Laravel\Cashier\Invoice class has __get method which proxies to Stripe\Invoice, so you can access it's properties on Laravel\Cashier\Invoice objects.

$model->invoices()->where('subscription', $subscription->stripe_id);

https://github.com/laravel/cashier/blob/10.0/src/Invoice.php#L345

By the way, this is not Eloquent, you're just calling where method on a collection:

https://laravel.com/docs/6.x/collections#method-where

illmatic's avatar

Thanks for the clarity, but that was actually my original attempt which didn't work.

Here is the flow

$subscription = $model->subscription($model->plan_name);
// dd($subscription->stripe_id);
// "sub_GdIMpEL1SCuuNH"
dd($model->invoices());

Removed some items for brevity

Collection {#572 ▼
  #items: array:1 [▼
    0 => Invoice {#540 ▼
      #owner: Business {#575 ▶}
      #invoice: Invoice {#569 ▼
        +saveWithParent: false
        id: "in_1G61m2LgRGJYZNdPW4si8j7d"
        object: "invoice"
        account_country: "US"
        account_name: "RitualShare"
        amount_due: 0
        auto_advance: false
        billing: "charge_automatically"
        billing_reason: "subscription_create"
        charge: null
        collection_method: "charge_automatically"
        created: 1580247546
        customer: "cus_GdIMgjUcxWlXyM"
        hosted_invoice_url: "https://pay.stripe.com/invoice/invst_MlxlNUvdnYd85b7CqdsMxKZ1rX"
        invoice_pdf: "https://pay.stripe.com/invoice/invst_MlxlNUvdnYd85b7CqdsMxKZ1rX/pdf"
        period_end: 1580247546
        period_start: 1580247546
        status_transitions: StripeObject {#573 ▶}
        subscription: "sub_GdIMpEL1SCuuNH"
        subtotal: 0
        webhooks_delivered_at: 1580247547
      }
      #items: null
    }
  ]
}

dd($model->invoices()->where('subscription',$subscription->stripe_id)); returns 0 items.

Sti3bas's avatar
Sti3bas
Best Answer
Level 53

@illmatic oh yeah, my mistake, sorry. where method will not work in this case, use filter instead:

$model->invoices()->filter(function($invoice) use ($subscription) {
   return $invoice->subscription === $subscription->stripe_id;
});

Please or to participate in this conversation.