Search code examples
phpwindowslaravel-5.7laravel-seedingend-of-line

PHP_EOL troubles in Laravel Seeder


I tried to run the following lines inside the run() function of a Laravel seeder.

$numbers = <<<EOL
1
2
3
4
EOL;

$array = explode(PHP_EOL, $numbers);

After run php artisan migrate:refresh --seed, I noticed that the $array value was:

Array (
  0 => '1
2
3
4',
)

The expected result was:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Why the explode() function within a seeder ignores the end of lines?


Solution

  • works fine

    $numbers = <<<EOL
    1
    2
    3
    4
    EOL;
    
    $array = explode("\n", $numbers)
    

    The purpose of PHP_EOL is to automatically choose the correct character for the platform. For DOS PHP_EOL - \r\n, mac - \r, unix - \n. In current situation u need to use statically \n.