How to resolve "array_key_exists() expects parameter 2 to be array, int given" in Laravel Redis
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
This article summarizes how to resolve the "array_key_exists() expects parameter 2 to be array, int given" error in Laravel Redis.
Environment Verified
PHP 7.4
Laravel 6
Predis 2.2
Cluster mode
Conclusion
The configuration in config/database.php is incorrect.
A number is present where an array is expected.
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
],
'clusters' => [
'default' => [
[
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'read_write_timeout' => 0,
],
],
],
],
The 'default' part was incorrect as follows.
// Example of incorrect configuration
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'read_write_timeout' => 0,
],
Structure of 'default' differs between cluster mode and normal mode
Normal Configuration
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'read_write_timeout' => 60,
],
Cluster Configuration
'default' => [
[
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
It is easier to notice the difference when looking at this part alone, but it can be difficult to notice in the midst of other configurations.
By the way, I encountered this issue because I mistakenly copied the configuration of the normal mode default when switching to cluster mode.
Summary
That's all for now. I hope this can be helpful to someone.
This blog is supported by clicks on advertisements. Thank you.
Goodbye!
Popular Articles
Deploying a PHP 7.4 + Laravel 6 Project to AWS EC2
Implementing Breadcrumbs in Laravel with laravel-breadcrumbs