Matthew Daly
Matthew Daly

Reputation: 9476

Cache::remember causing exception in Laravel 5

I'm struggling to cache a query in Laravel 5. I've written the following code, which looks consistent with what's in the documentation:

    // Get ID
    $id = Auth::id();

    // Get user
    $user = Cache::remember('user-' . $id, 5, function ($id) { 
        return User::find($id);
    });

But it raises the following error:

Missing argument 1 for App\Providers\RouteServiceProvider::{closure}()

Any idea where I've gone awry?

Upvotes: 1

Views: 737

Answers (1)

Margus Pala
Margus Pala

Reputation: 8663

Try this instead, mechanism for adding methods to closure functions is bit different than you are using.

$user = Cache::remember('user-' . $id, 5, function() use ($id) { 
    return User::find($id); //                       ^^^
});

Upvotes: 5

Related Questions