2
Undefined array key 1

  at D:\App-PHP\xampp_php_8\htdocs\test-project\vendor\laravel\framework\src\Illuminate\Foundation\Console\ServeCommand.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-PHP\xampp_php_8\htdocs\test-project\vendor\laravel\framework\src\Illuminate\Foundation\Console\ServeCommand.php:289
      Illuminate\Foundation\Bootstrap\HandleExceptions::Illuminate\Foundation\Bootstrap\{closure}("Undefined array key 1", "D:\App-PHP\xampp_php_8\htdocs\test-project\vendor\laravel\framework\src\Illuminate\Foundation\Console\ServeCommand.php")

  2   D:\App-PHP\xampp_php_8\htdocs\test-project\vendor\laravel\framework\src\Illuminate\Foundation\Console\ServeCommand.php:239
      Illuminate\Foundation\Console\ServeCommand::getDateFromLine("27.0.0.1:65342 Accepted")

D:\App-PHP\xampp_php_8\htdocs\test-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

akmal990
  • 21
  • 3

4 Answers4

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;
Saroj Shrestha
  • 2,696
  • 4
  • 21
  • 45
  • i mean, we dont need to change anything in servercommand.php in first place. and the app only show login form. it doesnt has any action at all. And it works normally on my local. So i dont know it return error.. – akmal990 Nov 02 '22 at 06:28
  • Is there any update on the above issue? Is it solved for anybody? – Tanvir Jul 26 '23 at 10:41
1

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();

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 04 '23 at 19:08
1

Change code function in your Servercommand.php file.

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]);
            }
            else{
                return Carbon::createFromFormat('D M d H:i:s Y', $matches);   
            }
    }

protected function getRequestPortFromLine($line)
    {
        preg_match('/:(\d+)\s(?:(?:\w+$)|(?:\[.*))/', $line, $matches);

        if (isset($matches[1])) {  
            return (int) $matches[1];
        }else{
            return (int) $matches;
        }
    }

0

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(); 
    
}