laravel 如何将变量传递给 Cache::remember 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39748706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to pass variable to Cache::remember function
提问by niko craft
Laravel docs give this example:
Laravel 文档给出了这个例子:
$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 that I get ErrorException in ImageController.php line 69: Undefined variable: hash
. I tried to pass $hash to function like so:
如果我运行,我得到ErrorException in ImageController.php line 69: Undefined variable: hash
. 我试图将 $hash 传递给这样的功能:
Cache::remember('thumb-'.$hash, 15, function($hash)
but then got another error as below:
但随后又出现了另一个错误,如下所示:
Missing argument 1 for App\Http\Controllers\ImageController::App\Http\Controllers{closure}(), called in C:\xampp\htdocs\imagesharing\vendor\laravel\framework\src\Illuminate\Cache\Repository.php on line 316 and defined
缺少 App\Http\Controllers\ImageController::App\Http\Controllers{closure}() 的参数 1,在 C:\xampp\htdocs\imagesharing\vendor\laravel\framework\src\Illuminate\Cache\Repository.php 中调用在第 316 行并定义
How do I pass argument so I can use it in my query?
如何传递参数以便我可以在查询中使用它?
回答by user1669496
You need to pass it using use
.
您需要使用use
.
Cache::remember('thumb-'.$hash, 15, function() use ($hash) {
$image = Image::where('hash', $hash)->first();
});