Variable-8 error with Cache::Remember() function

Asked

Viewed 21 times

0

My application is returning an error while storing the cache, I saw it was saving, but it is returning this error. Can anyone tell me why? Here is my function and error:

function that returns error:

       <?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();
        });
    }
}

error:

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

My . env with cache settings:

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

Does anyone know what it can be?

  • error due to lack of arguments, there is no way to know only with what you put, it is a local development error

  • @novic put more information in my question. Look if you know, please.

  • Seems to be the namespace take a look at where you use this because it seems there to be the problem!

  • @novic if you are interested I posted an answer with what was going wrong in my role. I thank you for the willingness to try to help me. Thank you very much. Beyoncé bless you.

1 answer

0

The problem was I was using function($ keywords), but you should be using function() use ($ keywords) because in the source code, you see that you’re asking $value = $callback(), but the function is waiting $keywords, if you want to share a value, you should use ($ Keywords) again, as your second Where function.

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

This solution obtained in stackoverflow.with I hope you help other people.

  • Just to complete you can use the https://laravel.com/docs/8x/helpers#method-cache . It would cache()->Remember.. cache()->get etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.