vitorccs / laravel-csv

PHP Laravel package to export and import CSV files in a memory-optimized way
MIT License
25 stars 5 forks source link

if row has array data then json encode it #2

Closed prageeth-peiris closed 1 year ago

vitorccs commented 1 year ago

Hi @prageeth-peiris

Thanks for your suggestion.

In case the cell is an array or an object, it is recommended to implement WithMapping, because we can set the formatting for each column:

public function map($row): array
{
  return [
    $row->id, // integer
    $row->name // string
    json_encode($row->my_array) // array, print as json: "["a","b","c"]"
    implode(', ', $row->my_array) // array, print in a user-friendly way: a, b, c
  ];
}

As you suggested in the source code, it would be nice to have a default formatting approach for arrays. However, I am not quite sure which (json/implode) would be the best.

I would vote on "implode" approach - I guess it it more likely we want to print the values in a user-friendly way.

prageeth-peiris commented 1 year ago

@vitorccs Thanks for your input !