laravel / nova-issues

554 stars 34 forks source link

Pivot fields of nova relations to the same resource in via the same pivot table #2625

Closed buttjer closed 3 years ago

buttjer commented 4 years ago

Description:

If you have a Nova Resource which is related mutliple times to the same model, via the same pivot table. And two BelongsToMany Fields and different pivot fields. You will displayed always the first pivot fields.

You would expect to have the input fields of the specific fields method. Not always the first. Both relation pivot forms are the same.

Steps To Reproduce:

Nova/Product.php


            BelongsToMany::make('Article', 'bundledArticles', Article::class)->fields(function () {
                    return [
                        Select::make('Type', 'type')->options([
                            'standard' => 'Standard',
                            'bundled' => 'Bundled,
                        ])->required()->rules('required'),
                        Text::make('goldlabel')
                    ];
                }),

            BelongsToMany::make('Article', 'articles', Article::class)->fields(function () {
                    return [
                        Select::make('Type', 'type')->options([
                            'standard' => 'Standard User',
                            'gold' => 'Gold User,
                        ])->required()->rules('required')
                    ];
                }),

Product.php


    public function articles()
    {
        return $this->belongsToMany(Article::class)
            ->withPivot('type', 'quantity')->wherePivotIn('type', ['standard']);
    }

    public function bundledArticles()
    {
        return $this->belongsToMany(Article::class)
            ->withPivot('type', 'quantity')->wherePivotIn('type', ['bundled']);
    }
buttjer commented 4 years ago

Maybe related to #1745

buttjer commented 4 years ago

related to #2128

SiebeVE commented 4 years ago

This issues is actually a much bigger thing then written here.

Not only will it take the first in the pivot fields, but when you actually edit a pivot field, it will update all pivot fields of other records to.

This results in the loss of data of all pivot fields.

sheggi commented 4 years ago

+1

This is still the case with Nova 3.7.1

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

f-liva commented 4 years ago

Any way to get this fixed?

SiebeVE commented 4 years ago

I can't see an easy way to fix this. I've fixed it with adding an ID to the rows of the pivot table. Then using that ID in the views for editing, deleting,... It is a rather heavy customisation that won't fit everyone needs.

IMPMAC commented 4 years ago

I am also experiencing this issue. If you have multiple belongsToMany with custom pivot fields, the pivot fields of other fields show up in error. The request to get the fields are a GET request to : /nova-api/products/creation-pivot-fields/products?editing=true&editMode=attach

So it seems it does not specify the relationship you are trying to get the pivot fields for so it can distinguish what fields to show up.

navidnadali commented 3 years ago

This issues is actually a much bigger thing then written here.

Not only will it take the first in the pivot fields, but when you actually edit a pivot field, it will update all pivot fields of other records to.

This results in the loss of data of all pivot fields.

Yeah this is pretty bad for our use case too as described above. Sorry @crynobone @davidhemphill is there any plans to look at a fix for this please?

crynobone commented 3 years ago

This issues is actually a much bigger thing then written here.

This perfectly describe the current state of the issue. I personally have try to look into this and it require major changes to the way pivot table is used in Laravel Nova.

crynobone commented 3 years ago

image

PR submitted but still working on adding additional tests for it.

crynobone commented 3 years ago

Fixed and released with v3.23.1.

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