I have a pivot table, its model is using the SoftDeletes
trait and its migration contains $table->softDeletes()
.
When using $thing->whatever()->detach($whateverId)
, the pivot's row is removed. Shouldn't the deleted_at
value be updated on the concerned row instead?
pivot tables are only a representation of a relation, if you need fields different from the relation field's, this is not a relation, is a new model/entity, so you need to use it like any other model and not has a relation.
But you can use the withTimestamps()
method when defining the relationship, which is maintaining the pivot's extra attributes (created_at
and updated_at
). I find it confusing.
Thanks for the report. Please can you provide the exact Laravel version you are using?
5.2.38
Thanks.
I'm not sure if this can be considered a bug, imho soft deletion is about deletion
of entities not pivot data, I don't think detach()
should just update the deleted_at
column, how would you restore the relation after soft deleting it then? Also how would I determine a specific relation as soft deletable? It shouldn't depend on the relation models, what if one model in the relation is soft deletable while the other is not?
I'm not sure if this can be considered a bug, imho soft deletion is about deletion of entities not pivot data
Why not? A relation should have the same incentive to be soft-deleted than an entity.
I don't think detach() should just update the deleted_at column, how would you restore the relation after soft deleting it then?
By calling restore()
which sets deleted_at
attribute to null. If the pivot is hard-deleted, there's no way to restore the relation without having to implement a custom log/audit to keep track of relations.
Also how would I determine a specific relation as soft deletable?
When using SoftDeletes
trait in the model.
It shouldn't depend on the relation models, what if one model in the relation is soft deletable while the other is not?
In this edge case the restoring should prevented. But this is probably a bad example since it makes the database schema not logical.
@sam-g I think you're answering based on a different situation, not the one in the issue.
The issue is about allowing detach()
to soft delete the relation, even if the relation models aren't deleted at all, that way restore()
won't be used, also how would you restore the relation of this model and not that model after soft deletion?
@sam-g soft deleting the model won't affect pivot data, meaning when you restore the model you'll have your relations as they were before deletion, that's why I don't see a point in soft deleting pivot records.
@themsaid This makes sense. I assumed an existing relation to a soft-deleted object would be invalid. Thanks for the explanation.
@GrahamCampbell this issue (https://github.com/laravel/framework/issues/14008) has not been resolved
Hello,
i know it's closed but i see a point to the soft deletion of pivot :
when i'm removing a relation between two entities, i want to keep a trace of this deletion,
i'm not removing any of the entities, just the relation between them, but i think it's important to know that these entites were previously related.
for my point of view, it's important to keep track of every action in the application, even when deleting pivot.
I'm using a closure in the delete and restore event to handle the deleted_at, but it's a little tricky, and it's not working when i just do a sync.
thanks.
Soft deletes aren't currently intended for relational data. This thread is about a feature request, not a bug. If you'd like to track relations, you may look for a package that tracks historical record of those things.
Update: To be clear, I'm saying the currently implementation is not intended to support soft deleting _the relationship between two entities, only the entities themselves_. So this issue is a feature request not a bug. Not stating my opinion, merely stating the facts based on current circumstances.
You can create a hasMany relation to the pivot table, and if accessing that relation and calling delete, it will be soft deleted.
Created my own solution for this, covered by tests:
https://github.com/ddzobov/laravel-pivot-softdeletes.git
in case you won't retrieving any softdeleted pivot table, add ->where('tables.deleted_at', NULL);
on your relation functions; work well on me
Most helpful comment
Hello,
i know it's closed but i see a point to the soft deletion of pivot :
when i'm removing a relation between two entities, i want to keep a trace of this deletion,
i'm not removing any of the entities, just the relation between them, but i think it's important to know that these entites were previously related.
for my point of view, it's important to keep track of every action in the application, even when deleting pivot.
I'm using a closure in the delete and restore event to handle the deleted_at, but it's a little tricky, and it's not working when i just do a sync.
thanks.