laravel / nova-issues

554 stars 34 forks source link

Can not use computed values with Heading field #5999

Closed alies-dev closed 1 year ago

alies-dev commented 1 year ago

Related issue: https://github.com/laravel/nova-issues/issues/2685

Description:

It's not possible to use computed fields with Heading field type. I tried different approaches:

Heading::make('Computed heading (label)', function () {
    return 'Computed heading (value)';
}),

Heading::make('Computed heading (label)', null, function () {
    return 'Computed heading (value)';
}),

Heading::make(function () {
    return 'Computed heading (value)';
}),

Heading::make('Computed heading (label)')->displayUsing(function () {
    return 'Computed heading (value)';
}),

Heading::make('Computed heading (label)')->fillUsing(function () {
    return 'Computed heading (value)';
}),

nothing works. The issue is in \Laravel\Nova\Fields\Heading::__construct: we need to remove $this->withMeta(['value' => $name]); line

Why do I need it: prevent heavy value computing on index page (I need to compute this value for details page only)

crynobone commented 1 year ago

The Heading field doesn't support computed usage, will consider throwing exception in upcoming version.

alies-dev commented 1 year ago

@crynobone thanks! That also means we need to update PHPDoc, in the current version it's confusing:


/**
 * @method static static make(mixed $name, string|null $attribute = null, callable|null $resolveCallback = null)
 */
class Heading extends Field implements Unfillable
``
crynobone commented 1 year ago

ResolvingCallback isn't ComputedCallback.

alies-dev commented 1 year ago

@crynobone

ResolvingCallback isn't ComputedCallback.

I know, but this code also doesn't work as expected:

Heading::make('Computed heading (label)', null, function () {
    return 'Computed heading (value)';
}),

it sets $resolveCallback, but it's never used on frontend. That's why I called it "confusing"

alies-dev commented 1 year ago

closing because:

The Heading field doesn't support computed usage, will consider throwing exception in upcoming version.