skip to Main Content
Undefined array key 1

  at D:App-PHPxampp_php_8htdocstest-projectvendorlaravelframeworksrcIlluminateFoundationConsoleServeCommand.php:289
    285▕     protected function getDateFromLine($line)
    286▕     {
    287▕         preg_match('/^[([^]]+)]/', $line, $matches);
    288▕
  ➜ 289▕         return Carbon::createFromFormat('D M d H:i:s Y', $matches[1]);
    290▕     }
    291▕
    292▕     /**
    293▕      * Get the request port from the given PHP server output.

  1   D:App-PHPxampp_php_8htdocstest-projectvendorlaravelframeworksrcIlluminateFoundationConsoleServeCommand.php:289
      IlluminateFoundationBootstrapHandleExceptions::IlluminateFoundationBootstrap{closure}("Undefined array key 1", "D:App-PHPxampp_php_8htdocstest-projectvendorlaravelframeworksrcIlluminateFoundationConsoleServeCommand.php")

  2   D:App-PHPxampp_php_8htdocstest-projectvendorlaravelframeworksrcIlluminateFoundationConsoleServeCommand.php:239
      IlluminateFoundationConsoleServeCommand::getDateFromLine("27.0.0.1:65342 Accepted")

D:App-PHPxampp_php_8htdocstest-project>

i m running laravel 9 locally alongside xampp-windows-x64-8.1.6-0-VS16-installer, using php artisan serve, and then it works normal. But when i m running on windows server alongside xampp as same as my local with same laravel app, the first reload runs normal, but the second reload it show error Undefined array key 1 on the cmd console, and the app stop working. I never face this kind of error on my previous laravel app running on windows server, btw my prev app running is version 8, this my first laravel 9 app, on windows server. thx before

3

Answers


  1. It basically showing that your variable $matches do not have index [1]. You either have to check the condition if it is isset or not or, if it should have beem not null, you need to check some code before it and verify, why it is undefined. For now, you could do like this:

    return isset($matches[1]) ? Carbon::createFromFormat('D M d H:i:s Y', $matches[1]) : null;
    
    Login or Signup to reply.
  2. Try out this solution it works for me.

    return isset($matches[1]) ? Carbon::createFromFormat(‘D M d H:i:s Y’, $matches[1]) : Carbon::now();

    Login or Signup to reply.
  3. change your code to:

    protected function getDateFromLine($line)
    {
        $regex = env('PHP_CLI_SERVER_WORKERS', 1) > 1
            ? '/^[d+]s[([a-zA-Z0-9: ]+)]/'
            : '/^[([^]]+)]/';
    
        preg_match($regex, $line, $matches);
    
        if (isset($matches[1])) {  
            return Carbon::createFromFormat('D M d H:i:s Y', $matches[1]);
        }
        return Carbon::now(); 
        
    }    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search