Aayush Dahal
Aayush Dahal

Reputation: 1182

Class "Redis" Not Found laravel 8, using phpredis

I am using laravel 8, and it is throwing error, when ever I tried to run:

php artsian config:clear

On laravel, this is my config value (because I am trying to use phpredis):

'client' => env('REDIS_CLIENT', 'phpredis'),

I could use redis-cli and ping at the moment. Not, just that I could hit the following endpoint successfully.

    public function testRedis()
    {
        Redis::set('ping','pong');
        $ping = Redis::get('ping');
        dd($ping);
    }

It prints out pong successfully.

but I am receiving class Redis not found. Whenever I tried to run, php artisan config:clear

Full error looks like this:

  Class "Redis" not found

  at vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:75
     71▕      * @throws \LogicException
     72▕      */
     73▕     protected function createClient(array $config)
     74▕     {
  ➜  75▕         return tap(new Redis, function ($client) use ($config) {
     76▕             if ($client instanceof RedisFacade) {
     77▕                 throw new LogicException(
     78▕                     extension_loaded('redis')
     79▕                         ? 'Please remove or rename the Redis facade alias in your "app" configuration file in order to avoid collision with the PHP Redis extension.'

      +10 vendor frames 
  11  app/Models/Setting.php:34
      Illuminate\Support\Facades\Facade::__callStatic()

  12  app/Providers/AppServiceProvider.php:37
      App\Models\Setting::getCachedValue()

and my App\Models\Setting looks like:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class Setting extends Model
{
    use HasFactory;
    
    protected $table = 'settings';
    protected $guarded = ['id'];
    protected $dates = ['created_at','updated_at'];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'created_at' => 'datetime:Y-m-d', // Change your format
        'updated_at' => 'datetime:Y-m-d',
    ];

    const STRIPE_ENVIRONMENT = ['test', 'live'];

    public static function getCachedValue(){
        return Cache::rememberForever('settings', function () {
            return Setting::pluck('key_value', 'key_name');
        });
    }

    public static function updateCachedSettingsData(){
        Cache::forget('settings');
        self::getCachedValue();
    }
}

What, I might be doing wrong here. #PS: This line on my config/app is commented.

// 'Redis' => Illuminate\Support\Facades\Redis::class,

Upvotes: 33

Views: 59319

Answers (6)

Alp Altunel
Alp Altunel

Reputation: 3443

for such issues, please delete the ./bootstrap/cache/*.php files and run command again.

Upvotes: 0

kovarov
kovarov

Reputation: 802

you must specify "REDIS CLIENT" in your .env file after installation

npm install predis/predis

in your .env file add this lines

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_CLIENT=predis

Upvotes: 0

Dushmanta
Dushmanta

Reputation: 61

I had the same issue, and I was able to resolve it by installing Redis and Redis Server. Follow these steps:

Install Redis for PHP:

sudo apt-get install php8.3-redis

Check your PHP version using php -v and substitute 8.3 with your actual PHP version.

Install Redis Server:

sudo apt install redis-server

Upvotes: 4

Hossein Piri
Hossein Piri

Reputation: 832

add this to app.php file

  'aliases' => Facade::defaultAliases()->merge([
        'Redis' => Illuminate\Support\Facades\Redis::class,
    ])->toArray(),

Upvotes: 0

kovarov
kovarov

Reputation: 802

you must specify "REDIS CLIENT" in your .env file after installation

composer require predis/predis

in the .env file add these lines

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_CLIENT=predis

Upvotes: 59

Aayush Dahal
Aayush Dahal

Reputation: 1182

Okay, I found the issue, there was updated pushed to ubuntu, and with this update looks like default php was updated to php8.0, previously it was 7.4. So, running following command fix the issue.

sudo apt-get install php8.0-redis

It looks like it was missing redis extension for php 8.0

Upvotes: 22

Related Questions