How to pass a variable to Cache :: remember function

The Laravel docs provides an example:

$value = Cache::remember('users', $minutes, function() { return DB::table('users')->get(); }); 

In my case, I have

 public function thumb($hash, $extension) { Cache::remember('thumb-'.$hash, 15, function() { $image = Image::where('hash', $hash)->first(); }); 

If I run, I get an ErrorException in ImageController.php line 69: Undefined variable: hash . I tried passing $ hash like this:

 Cache::remember('thumb-'.$hash, 15, function($hash) 

but then received another error, as shown below:

Argument 1 for App \ Http \ Controllers \ ImageController :: App \ Http \ Controllers {close} (), called in C: \ xampp \ htdocs \ imagesharing \ vendor \ laravel \ framework \ src \ Illuminate \ Cache \ Repository.php is missing on line 316 and defined

How to pass an argument so that I can use it in my request?

+5
source share
1 answer

You need to pass it using use .

 Cache::remember('thumb-'.$hash, 15, function() use ($hash) { $image = Image::where('hash', $hash)->first(); }); 
+12
source

Source: https://habr.com/ru/post/1257396/


All Articles