nova-issues icon indicating copy to clipboard operation
nova-issues copied to clipboard

allowDuplicateRelations detach not working as expected

Open potsky opened this issue 10 months ago • 9 comments

  • Laravel Version: 11.44.2
  • Nova Version: 5.5.4
  • PHP Version: 8.3.20
  • Database Driver & Version: PG
  • Reproduction Repository: https://github.com/potsky/nova

Description:

Same as #6496. Deleting a relation deletes all relations.

https://cln.sh/C6wChd7rBFGpMy6rVSkZ

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

  • https://github.com/potsky/nova
  • composer install
  • php artisan migrate --seed
  • Go to https://nova.test/nova/resources/contract-resources/1 with [email protected] / password
  • Delete a resource, it deletes all related resources.

potsky avatar May 07 '25 14:05 potsky

Unable to access https://github.com/potsky/nova

crynobone avatar May 07 '25 22:05 crynobone

Done public now

potsky avatar May 07 '25 22:05 potsky

Any update on this?

potsky avatar May 15 '25 16:05 potsky

Any update on this @crynobone ?

potsky avatar Jun 04 '25 07:06 potsky

At the moment, the code is working as expected.

BelongsToMany::make('Items', 'items', ItemResource::class)
                ->fields(new ContractItemFields())
                ->allowDuplicateRelations()
                ->withSubtitles()
                ->searchable()
                ->referToPivotAs('Scope'),

The 3rd parameter refers to ItemResource and doesn't have knowledge about ContactResource. As conclusion, it would be using definition from Item model to resolve everything instead of Contact model.

crynobone avatar Jun 04 '25 08:06 crynobone

At the moment, the code is working as expected.

BelongsToMany::make('Items', 'items', ItemResource::class) ->fields(new ContractItemFields()) ->allowDuplicateRelations() ->withSubtitles() ->searchable() ->referToPivotAs('Scope'), The 3rd parameter refers to ItemResource and doesn't have knowledge about ContactResource. As conclusion, it would be using definition from Item model to resolve everything instead of Contact model.

Yes so it should take into account the id in ->withPivot('id', 'role') and not deleting all resources...

You have written this here : https://github.com/laravel/nova-issues/issues/6496#issuecomment-2275281262

potsky avatar Jun 04 '25 09:06 potsky

The problem is on your Item model it doesn't have those definition:

Image

crynobone avatar Jun 04 '25 10:06 crynobone

Ok, I have added the same in Item, same behaviour.

potsky avatar Jun 04 '25 12:06 potsky

Any ETA @crynobone please ?

sylouuu avatar Jun 17 '25 14:06 sylouuu