outl1ne / nova-settings

A Laravel Nova tool for editing custom settings using native Nova fields.
MIT License
274 stars 96 forks source link

BooleanGroup field is not working #19

Closed elijahworkz closed 3 years ago

elijahworkz commented 4 years ago

I've tried to use BooleanGroup field in the settings and got this error: Method Illuminate\Support\Collection::isAssoc does not exist.

I found this issue that describes the problem - https://github.com/laravel/nova-issues/issues/2332

It's looks like they have solved this problem for the laravel nova by moving the macros to smaller services which are called separately in NovaServiceProvider.php boot(): $this->registerCarbonMacros(); $this->registerCollectionMacros();

but they are called AFTER $this->registerTools() and I think that is the reason why I have this error - the macro is not available yet. I can solve this problem by registering the macros before NovaSettings::setSettingsFields, but while BooleanGroup does appear, it's still not working properly - All the options appear checked, but the actual json value of options is not

liorocks commented 4 years ago

@elijahworkz ,

You are correct, registering macros before NovaSettings::addSettingsFields method works, however you need to cast that property to array. It is mentioned in Nova 2.0 Docs, but not on Nova 3.0

Example:

\Illuminate\Support\Collection::macro('isAssoc', function () {
  return \Illuminate\Support\Arr::isAssoc($this->toBase()->all());
});

\OptimistDigital\NovaSettings\NovaSettings::addSettingsFields([
  BooleanGroup::make('Permissions')->options([
    'create' => 'Create',
    'read' => 'Read',
    'update' => 'Update',
    'delete' => 'Delete',
  ]),
], ['permissions' => 'array']);
Tarpsvo commented 4 years ago

Ah yes, that's a bummer.

I don't think there's a way for me to fix this on the package side, but there already is a cleaner workaround - by using a callable.

This works perfectly, for example:

NovaSettings::addSettingsFields(fn () => [
    BooleanGroup::make('Permissions')->options([
        'create' => 'Create',
        'read' => 'Read',
        'update' => 'Update',
        'delete' => 'Delete',
    ]),
], ['permissions' => 'array']);

or for PHP version less than 7.4:

NovaSettings::addSettingsFields(function () {
    return [
        BooleanGroup::make('Permissions')->options([
            'create' => 'Create',
            'read' => 'Read',
            'update' => 'Update',
            'delete' => 'Delete',
        ]),
    ];
}, ['permissions' => 'array']);