Laravel收到错误消息“试图访问类型为int的值的数组偏移量”,同时调用aritsan命令Artisan :: call'cache:clear';

问题描述

我正在尝试使用代码清除缓存。它引发了一个错误Trying to access array offset on value of type int

Route::get('/clear-cache',function() {
    Artisan::call('cache:clear');
    return "Cache is cleared";
});

elseif ('-' === $key[0])行中的错误

protected function parse()
{
    foreach ($this->parameters as $key => $value) {
        if ('--' === $key) {
            return;
        }
        if (0 === strpos($key,'--')) {
            $this->addLongOption(substr($key,2),$value);
        } elseif ('-' === $key[0]) {
            $this->addShortOption(substr($key,1),$value);
        } else {
            $this->addArgument($key,$value);
        }
    }
}

解决方法

键变量现在可能不是数组。您可以显式地将其转换为数组

protected function parse()
{
    foreach ($this->parameters as $key => $value) {
        $key = (array)$key;
        if ('--' === $key) {
            return;
        }
        if (0 === strpos($key,'--')) {
            $this->addLongOption(substr($key,2),$value);
        } elseif ('-' === $key[0]) {
            $this->addShortOption(substr($key,1),$value);
        } else {
            $this->addArgument($key,$value);
        }
    }
}