Soft delete on a intermediate table for many-to-many relationship

前端 未结 3 994
孤独总比滥情好
孤独总比滥情好 2020-12-12 22:35

How do I set soft delete on an intermediate table which is connecting two different types of entities? I\'ve added deleted_at column, but the docs say that I need to put th

3条回答
  •  情话喂你
    2020-12-12 23:35

    As far as I understand it; an intermediate table is simply a length of string attaching one tables record to a record in another table and as such it does not require a soft delete method.

    To explain, imagine you have a Users table and a Groups table, each user can have more than one Group and each Group can belong to more than one User. Your pivot table may be User_Group or something like that and it simply contains two columns user_id and group_id.

    Your User table and Group table should have a deleted_at column for soft deletes, so when you "delete" say a Group, that group association will not appear in $User->Groups() while the pivot table row has remained unaffected. If you then restore that deleted Group, it will once again appear in $User->Groups().

    The pivot table row should only be affected if that group record is hard deleted, in which case the pivot rows should also be hard deleted.

    Now I have explained why I do not believe you need to add soft delete to a pivot table; is there still a reason why you need this behavior?

提交回复
热议问题