Home > OS >  Import UTF-8 BOM CSV using Laravel Excel package
Import UTF-8 BOM CSV using Laravel Excel package

Time:08-19

I'm trying to import a simple CSV file using the [SpartnerNL/Laravel-Excel][1] package. The CSV looks like this:

Name,Time,Amount (ml),Note
Dave Smith,"14/08/2022, 22:18",60
Sarah Jones,"14/08/2022, 18:00",100
Mark Brown,"14/08/2022, 13:54",100

My import file contains this function:

public function model(array $row)
{
    dd($row);
}

However the file is not parsed properly, the above code returns:

array:3 [
  0 => "Name,Time,Amount"
  1 => "(ml),Note"
  2 => null
]

When I query the file using Terminal and get the following:

'my_file.csv: Unicode text, UTF-8 (with BOM) text'

Its definitely related to the encoding because if I resave the csv as "csv text" it parses properly. Changing the "input_encoding" to "UTF-8" and "use_bom" to true makes no difference. The CSV is output by a program I do not control so cannot change the format or contents. How can I properly import a UTF-8 BOM csv into Laravel?

CodePudding user response:

Needed to add the delimiter in the excel.php config file, change

'delimiter'        => '',

to

'delimiter'        => ',',
  • Related