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

theUnforgiven's avatar

Cache results with Stripe/Cashier

Hi All,

I have a repository that has a method in it like so:

public function allInvoices()
{
        $user = Auth::user();
        $invoices = $user->invoices();

        return $invoices;
}

Which is injected into my controller, then I have a method in the controller like:

public function getInvoices()
{
        $invoices = $this->provider->allInvoices();
        return View::make('provider.invoices', compact('invoices'));
}

How can I use Cache remember function to cache this for a few days before making another API request to Stripe? Obviously based on my code, and I still need to pass through to the view, if any can help me with this and use my code to make the Cache work.

I am using Cashier for this and not standalone Stripe.

Thanks in advance.

0 likes
9 replies
bashy's avatar
$invoices = Cache::remember('invoices', '259200', function()
{
    return $this->provider->allInvoices();
});
1 like
bashy's avatar

Any reason you need to cache the view file?

If you need to, just add it into the closure and return the view in with it.

$invoices = Cache::remember('invoices', '259200', function()
{
    $invoices = $this->provider->allInvoices();
    return View::make('provider.invoices', compact('invoices'));
});

return $invoices;

Will be dodgy since caching the whole view will make it non dynamic.

1 like
theUnforgiven's avatar

Simply because I need the view to render the page, or is there another way say in the repo? (see above)

bashy's avatar
bashy
Best Answer
Level 65

Yeah so why not just

$invoices = Cache::remember('invoices', '259200', function()
{
    return $this->provider->allInvoices();
});

return View::make('provider.invoices', compact('invoices'));

$invoices will always be the same

theUnforgiven's avatar

No that's then not rendering and causing this error. Invalid argument supplied for foreach() cos it's not bee passed to the view right?

bashy's avatar

Had an error on my end, updated my last reply. Also if you want to use vars inside the closure, you'll need to do

use ($var)
theUnforgiven's avatar

Yeah @bashy that's the one had a thought it be be return $this->provider.... but just wanted extra opinion, cheers man!

Please or to participate in this conversation.