Skip to content
This repository has been archived by the owner on Apr 18, 2020. It is now read-only.

Illegal offset type #1

Closed
kombimedia opened this issue Sep 25, 2018 · 5 comments
Closed

Illegal offset type #1

kombimedia opened this issue Sep 25, 2018 · 5 comments

Comments

@kombimedia
Copy link

Hi @Sparclex ,

Thanks for the nice card!

However I am having an issue importing to a resource that has a computed field, receive an 'illegal offset type error'. If I comment out the computed field in the resource, everything works as expected.

Error message:
Illegal offset type {"userId":4,"email":"email@email.com","exception":"[object] (ErrorException(code: 0): Illegal offset type at /home/vagrant/websites/shoeboxchristmas/vendor/laravel/nova/src/Fields/Field.php:362)

There also seems to be an issue importing to a 'BelongsTo' field, unless it is not marked as 'required' in the resource. I need this to be a required field though.

Any ideas on fixes or work arounds for these issues?

Also are you considering adding field mapping? That would be really nice.. : )

A sample import file is attached (had to save it as 'xlsx' to attach).

Thank you in advance for any assistance.

This is my resource;

`<?php

namespace App\Nova;

use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Http\Requests\NovaRequest;
use App\Nova\Metrics\Children;
use App\Nova\Metrics\Donators;
use App\Nova\Metrics\RegisteredShoeBoxes;
use Sparclex\NovaImportCard\NovaImportCard;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Fields\Select;
use Naif\Toggle\Toggle;
use App\School;
use Maatwebsite\LaravelNovaExcel\Actions\DownloadExcel;

class Child extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = 'App\Child';

/**
 * The single value that should be used to represent the resource when being displayed.
 *
 * @var string
 */
public static $title = 'name';

/**
 * The columns that should be searched.
 *
 * @var array
 */
public static $search = [
    'id', 'name', 'donator_id', 'contact_name', 'contact_number',
];

/**
* The relationships that should be eager loaded on index queries.
*
* @var array
*/
public static $with = ['school', 'donator'];

/**
 * Get the fields displayed by the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function fields(Request $request)
{
    return [
        ID::make()
            ->sortable()
            ->hideFromIndex(),

        Text::make('Ref', function () {
            $id = $this->school_id;
            $schools = School::where('id', $id)->get();
            foreach ($schools as $school) {
                return $school->code. '' .$this->id;
            }
        }),

        Text::make('Name')
            ->sortable()
            ->rules('required', 'max:255'),

        BelongsTo::make('School')
            ->rules('required'),

        Text::make('Age')
            ->sortable()
            ->rules('required', 'max:3'),

        Select::make('Gender')
            ->options([
                'Girl' => 'Girl',
                'Boy' => 'Boy',
            ])
            ->sortable()
            ->rules('required'),

        Textarea::make('Notes')
            ->rules('max:500'),

        Text::make('Contact Name')
            ->rules('required', 'max:255'),

        Text::make('Contact Number')
            ->rules('required', 'max:55'),

        BelongsTo::make('Donator')
            ->hideWhenCreating()
            ->searchable(),

        Toggle::make('Allocated')
            ->color('#3C8D0D')
            ->hideWhenCreating()
            ->sortable(),
    ];
}

/**
 * Get the cards available for the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function cards(Request $request)
{
    return [
        (new Donators)->width('1/3'),
        (new Children)->width('1/3'),
        (new RegisteredShoeBoxes)->width('1/3'),
        new \Sparclex\NovaImportCard\NovaImportCard(Child::class),
    ];
}

/**
 * Get the filters available for the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function filters(Request $request)
{
    return [];
}

/**
 * Get the lenses available for the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function lenses(Request $request)
{
    return [];
}

/**
 * Get the actions available for the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function actions(Request $request)
{
    return [
        (new DownloadExcel)
            ->withHeadings()
            ->allFields()
            ->except('school_code', 'donator_id', 'deleted_at', 'created_at', 'updated_at')
            ->askForFilename(),
    ];
}

}
`

child-import-test.xlsx

@Sparclex
Copy link
Owner

Sparclex commented Sep 25, 2018

Thank you for the detailed description of your issues. I just published a new release with the fixes. To get it working, you need to rename school_id to school or however your relation is called in your model.

Also are you considering adding field mapping? That would be really nice.. : )

How do you imagine that? Do you want to be able to have different column names in the csv than in the database?

@kombimedia
Copy link
Author

@Sparclex

Fantastic response time, thank you so much! both issues sorted, thank you.

Regarding mapping fields. As long as I make a csv template available to users, it is probably fine without mapping. However, I think it would be nice for users to be able to visually define the columns that they are required to populate. Particularly if they are receiving an sql error that they don't understand. The mapping would indicate the correct columns and column names, or they can just select the appropriate column to map to.

Import Card is great either way.

Cheers,

@Sparclex
Copy link
Owner

@kombimedia

Thank you for the feedback. I am happy to help and improve the package.

If you define all your rules correctly, the user should not receive any sql errors. I validate the data the same way as it would be validated when creating a resource via the Nova UI. However at the moment I am only able to display one validation error due to the constraints of the card height.
image

If I would figure something out to display all validation errors, would that be a good enough indication for the user?

I try to prevent implementing column mapping, since I want to extend the package for different import file types where the column mapping might not be suitable.

Cheers,

@kombimedia
Copy link
Author

@Sparclex

I think you're right. With validation configured correctly a user should be able to troubleshoot any issues with their import file. This will work for my situation.

One suggestion with the error output could be to make it a little more readable? i.e. instead of outputting the row index then the column that needs attention, it could output something like

The value field in row 5 is required

Currently this would be output as;

The 3.value field is required

With the heading row ignored then 0 index for the first data row, A user may not understand that 3 is in fact row 5 in their import file.

Just a thought.

Cheers,

@Sparclex
Copy link
Owner

I didn't find a way yet to modify the validation messages for an array validation (https://laravel.com/docs/5.6/validation#validating-arrays) where I can access the index. But if I don't, I will just do the validation loops (for every row) manually. If you know an easier way, feel free to share.

But please continue the discussion in #2 since this is a seperate issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants