Class "Redis" Not Found laravel 8, using phpredis
Asked Answered
W

5

25

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,
Willawillabella answered 23/11, 2021 at 5:32 Comment(1)
It looks like the PHP Redis extension is not installed, You can check if the Redis PHP extension is installed & enable. Redis is installed, configured & running.Snailpaced
O
52

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
Occupant answered 30/12, 2022 at 20:37 Comment(3)
Thanks, REDIS_CLIENT=predis was missingWesleywesleyan
in the database.php file, replace the following line: 'client' => env('REDIS_CLIENT', 'phpredis') to: 'client' => env('REDIS_CLIENT', 'predis')Tidemark
For Windows users this was the best answer ever! Just 2 steps and everything worked as it should! Thanks @OccupantEisteddfod
W
18

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

Willawillabella answered 23/11, 2021 at 6:44 Comment(4)
Unable to locate package php8.1-redisAlwitt
@Alwitt me too having same issue unable to locateAric
sudo apt-get install php8.1-redis seems to work for me Ubuntu 20.x LTSZalea
Just look the package name with apt search php | grep redis. then install the packageSnailpaced
H
0

add this to app.php file

  'aliases' => Facade::defaultAliases()->merge([
        'Redis' => Illuminate\Support\Facades\Redis::class,
    ])->toArray(),
Hanus answered 20/11, 2023 at 22:39 Comment(0)
G
0

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
Gravely answered 30/1 at 11:12 Comment(0)
O
0

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
Occupant answered 2/3 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.