Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Illegal offset type when Resource has Custom keys with array type #13

Open
tumainimosha opened this issue Aug 6, 2019 · 2 comments
Open
Labels
awaiting response bug Something isn't working

Comments

@tumainimosha
Copy link

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

tumainimosha added a commit to tumainimosha/laravel-nova-csv-import that referenced this issue Aug 6, 2019
@simonhamp simonhamp added the bug Something isn't working label Oct 25, 2019
@simonhamp
Copy link
Owner

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?

@monaye
Copy link
Contributor

monaye commented Dec 4, 2020

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants