【问题标题】:Laravel-8 error with Cache::remember() function doesn't workCache::remember() 函数的 Laravel-8 错误不起作用
【发布时间】:2021-08-29 07:24:07
【问题描述】:

我的应用程序在存储缓存时返回错误,我看到它正在保存,但它正在返回此错误。谁能说出为什么? 这是我的功能和错误:

返回错误的函数:

    <?php

namespace App\Repositories\ProductFilter;

use App\Models\Product;
use App\Repositories\Contracts\IProductFilterRepository;
use Illuminate\Support\Facades\Cache;

class ProductFilterRepository implements IProductFilterRepository
{
   
    public function getProductsRecommendations($keywords)
    {
        $expiration = 10;
        $keyName = 'productsRecommended';

        return Cache::remember($keyName, $expiration, function ($keywords) {
            return  Product::query()->where(function ($productsAll) use ($keywords) {
                if ($keywords) {
                    foreach ($keywords as $keyword)
                        $productsAll->orWhere('title', 'LIKE', '%' . $keyword . '%')->orWhere('description', 'LIKE', '%' . $keyword . '%')->orWhere('code', 'LIKE', '%' . $keyword . '%');
                }
            })->where('status', 'active')->get();
        });
    }
}

错误:

ArgumentCountError
Too few arguments to function App\Repositories\ProductFilter\ProductFilterRepository::App\Repositories\ProductFilter\{closure}(), 0 passed in /var/www/vendor/laravel/framework/src/Illuminate/Cache/Repository.php on line 385 and exactly 1 expected
http://localhost:8888/recommended

带有缓存设置的我的 .env

BROADCAST_DRIVER=log
CACHE_DRIVER=redis
REDIS_URL=redis
QUEUE_CONNECTION=sync
SESSION_DRIVER=redis
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_CLIENT = redis

有谁知道它是什么?

【问题讨论】:

  • 这个可能的功能是什么App\Repositories\ProductFilter\ProductFilterRepository::App\Repositories\ProductFilter\{closure}()
  • 这是我的课。我编辑了我的问题并添加了完整的课程。

标签: php laravel caching redis laradock


【解决方案1】:

经过一番思考,我想我知道你的问题是什么,你使用的是function ($keywords),但你应该使用function () use ($keywords),因为在source code中,你看到它在做$value = $callback() ,但是你的函数正在等待$keywords,如果你想共享一个值,你必须再次使用use ($keywords),就像你在where中的第二个函数一样。

所以,应该是:

return Cache::remember($keyName, $expiration, function () use ($keywords) {

【讨论】:

    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 2017-02-06
    • 2014-05-17
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 2013-09-30
    相关资源
    最近更新 更多