Laravel Undefined array key 1 php artisan serve

3.5k views Asked by At
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

5

There are 5 answers

2
Saroj Shrestha On

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;
1
Luyolo Mnganga On

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

0
soheila sadeghian On

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(); 
    
}    
0
Mihir Vadoliya On

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

1
Arqam Ali On
if (isset(preg_match('/^\[([^\]]+)\]/', $line, $matches)) && 
    is_array(preg_match('/^\[([^\]]+)\]/', $line, $matches))) {
         preg_match('/^\[([^\]]+)\]/', $line, $matches);
         return Carbon::createFromFormat('D M d H:i:s Y', $matches[1]);
 } else {
         return;
 }