laravel-pivot-softdeletes icon indicating copy to clipboard operation
laravel-pivot-softdeletes copied to clipboard

sync() not restore already deleted row

Open Enotze opened this issue 3 years ago • 4 comments

Steps:

  1. ->sync([1,2,3]) - create 3 new rows in pivot
  2. ->sync([2,3]) - mark first row as deleted
  3. ->sync([1,2,3]) - fails, because it try to insert first row again, but must restore this

Enotze avatar Sep 09 '20 08:09 Enotze

It will create new row only if you dont have constraints in db for that.

If you want to restore row instead of create new one - use different method

ddzobov avatar Sep 09 '20 09:09 ddzobov

Maybe add new variative scope or parameter for control the scenario of work sync(): restore or insert new row, it's will be very cool and simplify develop

Enotze avatar Sep 09 '20 10:09 Enotze

Maybe add new variative scope or parameter for control the scenario of work sync(): restore or insert new row, it's will be very cool and simplify develop

Feel free to make PR for that

ddzobov avatar Oct 22 '20 21:10 ddzobov

+1 for this.

Thanks for a great library! It would be awesome if you could use sync() as normal and it would restore in one go. If this is too hard to implement, can anyone please guide me in the right direction of how I can restore what's needed and still use sync?

I have to check my array against the database and set all deleted_at to null and then run the sync after? Is it that simple?

Thanks!

Edit// This seems to work, unless restore with my whole array is doing any harm except for restoring existing posts where deleted_at is not null as well as changing the updated_at timestamp for non-deleted rows. Would still be cool to do this in one go, but if you don't see any other downsides in running it like this, I'm happy with it :) Thanks again!

$this->post->tags()->restore($this->tags);
$this->post->tags()->sync($this->tags);

seabasss avatar Sep 05 '21 16:09 seabasss