laravel / nova-issues

554 stars 34 forks source link

morphOne with default value fails loading in Nova #4733

Closed Ahrengot closed 2 years ago

Ahrengot commented 2 years ago

Description:

I have two models connected via a polymorphic relationship and I get a loading error if I load up a tenant in Nova that doesn't yet have a landing page. If I remove ->withDefault() from the relationship definition the error disappears.

Table structure

tenants id name
1 Foo Business
2 Bar Business
landing_pages id owner_type owner_id headline body
1 App\Models\Tenant 1 Learn all about Foo ...
2 App\Models\Tenant 2 Why Bar is the best ...

Eloquent relationships

in App\Models\Tenant.php

public function landingPage(): MorphOne
{
    return $this->morphOne(LandingPage::class, 'owner')
                ->withDefault();
}

in App\Models\LandingPage.php

public function owner(): MorphTo
{
    return $this->morphTo();
}

Nova resources

in App\Nova\Tenant.php

public function fields(NovaRequest $request)
{
    return [
        MorphOne::make('Landing Page'),
        ... More fields
    ];
}

in App\Nova\LandingPage.php

public function fields(NovaRequest $request): array
{
    return [
        MorphTo::make('Owner', 'owner')
               ->types([Tenant::class])
               ->sortable(),
        ... More fields
    ];
}

Detailed steps to reproduce the issue on a fresh Nova installation:

  1. Set up relationships as described above with the withDefault() chained after the relationship.
  2. Navigate to a Tenant/owner resource within Nova that doesn't yet have a connected landing page, so the ->withDefault() method is triggered.
  3. The following error appears:

Attempt to read property "nullable" on null in Laravel\Nova\Fields\ID:forResource


error-2


error-1
crynobone commented 2 years ago

I don't think I will be able to reproduce the issue, please provide full reproducing repository based on fresh installation as suggested in the bug report template (or you can refer to https://github.com/nova-issues for example)

crynobone commented 2 years ago

The only thing I can think of why this is happening is that your Landing resource doesn't have an ID field. But as mentioned earlier we would need a full reproducing repository to verify as there isn't enough information to verify the issue. We do have enough feature tests for ID field

Ahrengot commented 2 years ago

Thank you for for your help @crynobone

You're right! It was the missing id. If I remove this line: https://github.com/Ahrengot/morphone-nova-example/blob/master/app/Nova/LandingPage.php#L44 then the error appears.

Guess I'm never removing an ID column from a nova resource again 😀

crynobone commented 2 years ago

If possible retain the ID field as it made resolving the key for a resource will require fewer steps. But it is clear now where it can cause errors and how we can fix/improve this.

github-actions[bot] commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.