anaseqal / nova-import

Laravel Nova Import Action
72 stars 28 forks source link

Nova data import to JSON database fields from CSV / XLS file #34

Open LaravelLover069 opened 2 years ago

LaravelLover069 commented 2 years ago

Sharing a solution here for all those who attempt to use this package to import CSV or XLS(X) data into a JSON database field:

Use Case: Import cell values (e.g. address details) from CSV or XLS files into destination JSON columns.

Bildschirmfoto vom 2022-04-03 03-38-42

Solution: Wrap the array mapping between the database JSON child node and the CSV / XLS column within the following function:

response()->json([ Add array here ])->getData()

Place address code example:

public function model(array $row)
    {
        return new Client([
            'name' => $row['client'],
            'location' => response()->json([
                'address' => $row['address'],
                'street'=> $row['street'],
                'house_no'=> $row['house_no'],
                'postcode'=> $row['postcode'],
                'city'=> $row['city'],
                'county'=> $row['county'],
                'country'=> $row['country']
            ])->getData(),
        ]);

@anaseqal Would be great to have this somewhere in the readme / documentation as this might be useful for others too.