Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

Relationship field does not remove deleted related items from database. #706

Closed Uteev closed 8 months ago

Uteev commented 11 months ago

I have relationship field. It uses HasMany relation. When I remove related item and click "Save & Back" button it does not delete from database the removed related item.

I tried to debug the problem and I find out that the problem occurs in Backpack\CRUD\app\Library\CrudPanel\Traits\Create on the line 324

$entry->{$relationMethod}()->whereNotIn($relatedModelLocalKey, $relatedItemsSent)->lazy()->each->delete();

If I remove from this code lazy()->each so it become

$entry->{$relationMethod}()->whereNotIn($relatedModelLocalKey, $relatedItemsSent)->delete();

then it works perfectly.

I could not understand why lazy()->each not working.

promatik commented 11 months ago

Hi @Uteev! The lazy() method fetches the database entries using PHP generators, it should work as well.

I also tested this with our demo, both locally and in online: https://demo.backpackforlaravel.com/admin/monster/140/edit#relationship.

image

While adding new and removing old ones, it works.

I'm clueless, @pxpm, do you know any situation where ->lazy()->each could not work?

pxpm commented 11 months ago

Me too it seems to work on my side. @Uteev can you replicate this in our demo project: https://github.com/Laravel-Backpack/demo or in a clean install ? I want to exclude the possibility of being some project specific issue.

Cheers

tabacitu commented 10 months ago

Hey @Uteev we really want to get to the bottom of this. Can you please try to reproduce in our demo, so we know what to test?

pxpm commented 8 months ago

Closing due to inactivity. If the bug persist, please re-open.

Cheers