laravel-nova-csv-import
laravel-nova-csv-import copied to clipboard
Illegal offset type when Resource has Custom keys with array type
Getting below error when Importing
Illegal offset type {"userId":1,"exception":"[object] (ErrorException(code: 0): Illegal offset type at /path/to/project/vendor/simonhamp/laravel-nova-csv-import/src/Importer.php:133)
[stacktrace]
#0 /path/to/project/vendor/simonhamp/laravel-nova-csv-import/src/Importer.php(133): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'Illegal offset ...', '...', 133, Array)
...
I traced the issue down to this function in Importer.php
private function mapRowDataToAttributes($row)
{
$data = [];
foreach ($this->attributes as $field) {
$data[$field] = null;
...
The Nova Resource I was trying to import has a MapField, which in $this->attributes evaluates as an array. When checking for using offset array thats when it throws exception.
A quick solution i see is to check if key is string before using it.
$data = [];
foreach ($this->attributes as $field) {
// skip array attributes
if(!is_string($field)) continue;
$data[$field] = null;
...
I could do a PR for this if this is ok
Thanks for investigating and reporting @tumainimosha. I'm not familiar with this sort of MapField... are you using a specific Nova package to add this or did you roll your own? Could you share the implementation of that Field so that I can investigate further?
@simonhamp I think it's about having a array
casts on JSON field.
https://laravel.com/docs/8.x/eloquent-mutators#array-and-json-casting