laravel 多对多关系的中间表上的软删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17350072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:31:29  来源:igfitidea点击:

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

laravellaravel-4

提问by misaizdaleka

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 this into the model:

如何在连接两种不同类型实体的中间表上设置软删除?我添加了deleted_at 列,但文档说我需要将其放入模型中:

protected $softDelete = true;

Of course, I don't have a model for an intermediate table. Any idea?

当然,我没有中间表的模型。任何的想法?

回答by Ronald Hulshof

You can put a constraint on the Eager Load:

您可以对 Eager Load 施加约束:

public function groups()
    {

        return $this
        ->belongsToMany('Group')
        ->whereNull('group_user.deleted_at') // Table `group_user` has column `deleted_at`
        ->withTimestamps(); // Table `group_user` has columns: `created_at`, `updated_at`

    }

Instead of HARD deleting the relationship using:

而不是 HARD 删除关系使用:

User::find(1)->groups()->detach();

You should use something like this to SOFT delete instead:

你应该使用这样的东西来软删除:

DB::table('group_user')
    ->where('user_id', $user_id)
    ->where('group_id', $group_id)
    ->update(array('deleted_at' => DB::raw('NOW()')));

回答by jonny

You could also use Laravel's Eloquent BelongsToMany method updateExistingPivot.

你也可以使用 Laravel 的 Eloquent BelongsToMany 方法updateExistingPivot

$model->relation->updateExistingPivot($relatedId, ['deleted_at' => Carbon\Carbon::now()]);

So to use @RonaldHulshof examples you have a User model with a groups relationship which is a belongsToMany relationship.

因此,要使用@RonaldHulshof 示例,您有一个 User 模型,该模型具有一个属于属对多关系的组关系。

public function groups() {
    return $this->belongsToMany(Group::class)->whereNull('groups_users.deleted_at')->withTimestamps();
}

Then in order to soft delete the pivot table entry you would do the following.

然后为了软删除数据透视表条目,您将执行以下操作。

$user->groups->updateExistingPivot($groupId, ['deleted_at' => Carbon\Carbon::now()]);

回答by carbontwelve

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_Groupor something like that and it simply contains two columns user_idand group_id.

解释一下,假设您有一个 Users 表和一个 Groups 表,每个用户可以有多个组,每个组可以属于多个用户。您的数据透视表可能是User_Group或类似的东西,它只包含两列user_idgroup_id.

Your Usertable and Grouptable should have a deleted_atcolumn 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().

你的User表和Group表应该有一个deleted_at软删除列,所以当你“删除”一个组时,$User->Groups()当数据透视表行保持不受影响时,该组关联将不会出现。如果您随后恢复该已删除的组,它将再次出现在$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?

现在我已经解释了为什么我认为您不需要向数据透视表添加软删除;你还有什么需要这种行为的理由吗?