Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

babai9's avatar

How can I fix the error "Undefined array key 1" on Laravel artisan command

I am using laravel 10 a fresh installed project in xampp which is also newly installed, while running php artisan serve command I am getting the below error but not always it is showing randomly dont know how this error is showing. Laravel 8 or 9 doesnot gives this issue, it is only there in laravel 10. Everything loads normally but suddenly this error is showing randomly while loading the page. Please help me to fix this issue

Undefined array key 1

  at vendor\laravel\framework\src\Illuminate\Foundation\Console\ServeCommand.php:302
protected function getDateFromLine($line)
    {  
 $regex = env('PHP_CLI_SERVER_WORKERS', 1) > 1
            ? '/^\[\d+]\s\[([a-zA-Z0-9: ]+)\]/'
            : '/^\[([^\]]+)\]/';

        $line = str_replace('  ', ' ', $line);

        preg_match($regex, $line, $matches);

        return Carbon::createFromFormat('D M d H:i:s Y', $matches[1]);
   }
0 likes
14 replies
jaseofspades88's avatar

$matches is empty in some situations. What would you expect to return if your regex fails?

babai9's avatar

@jaseofspades88 I don't know what to do can you please suggest something? As i need to run the artisan command to complete my project.

jaseofspades88's avatar

@babai9 If you are using xamp, why are you using php artisan serve? Doesn't xamp provide you with your environment?

babai9's avatar

@jaseofspades88 For running the laravel project I am using artisan command as it is laravel way, I have previously used in other versions there was no issue

jaseofspades88's avatar

@babai9 I'd try refreshing your application. From root

rm -rf vendor
composer install 

The command you're having problems with is baked into Laravel

babai9's avatar

Hi does any one knows how to resolve it

napzky's avatar

@babai9 in my case, change .env file to minimum workers

PHP_CLI_SERVER_WORKERS=1
DhrutiPandyaOB's avatar

I don't know if I am late or not but hope it helps you

protected function getDateFromLine($line)
{
    $regex = env('PHP_CLI_SERVER_WORKERS', 1) > 1  ? '/^\[\d+]\s\[([a-zA-Z0-9: ]+)\]/'  : '/^\[([^\]]+)\]/';
    $line = str_replace('  ', ' ', $line);
    preg_match($regex, $line, $matches);
    if (isset($matches[1])) {
        return Carbon::createFromFormat('D M d H:i:s Y', $matches[1]);
    }
    return null;
}

Please or to participate in this conversation.