laravel / nova-issues

556 stars 34 forks source link

HasMany field in fieldsForDetail() will give 404 error when attempt to create #5749

Closed magdicom closed 1 year ago

magdicom commented 1 year ago

Description:

I'm using Resource::fieldsForDetail to display certain fields different from what is displayed in index and update resource form. when defining the relationship field HasMany inside the Resource::fieldsForDetail method the table will load normally, but the problem is when I click the Create XX button above this relationship table it will keeps loading and it will return a 404 error page at the logs.

The fix: after hours of tweaking this I figured it could only work if the same relationship field is defined in the fields method, even if its hidden.

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

to reproduce this error you simply need to define a HasMany field in one resources' fieldsForDetail:

// following the example given in docs: https://nova.laravel.com/docs/4.0/resources/relationships.html#hasmany

class User extends Resource
{
    public function fields(NovaRequest $request)
    {
        return [
            Text::make('Name'),
        ];
    }

    public function fieldsForDetail(NovaRequest $request)
    {
        return [
            HasMany::make('Posts'),
        ];
    }
}

the example above wont work when you try to create a new post from inside user details page.

crynobone commented 1 year ago

This PR has been reverted, please add HasMany field to fieldsForCreate() or fields() if you need to use this structure.

sentq commented 1 year ago

@crynobone what you say actually is that I'm forced to use fields() at all cases and I cannot specify fields in different methods like fieldsForDetails and fieldsForIndex!

crynobone commented 1 year ago

I would recommend using fields() and only uses dynamic fields for further customisation. Internally for relations checking we would prefer to filters all dynamic fields to avoid false positive and avoid performance penalty when we need to check if the field actually exists in a resource.

magdicom commented 1 year ago

I see your point, but I wish you would reconsider this even later.

also if this can be mentioned in the docs it would be very time savior.

Thanks!

github-actions[bot] commented 1 year 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.