Laravel Eloquent ORM - 多对多删除剩余的数据透视表值 [英] Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

查看:29
本文介绍了Laravel Eloquent ORM - 多对多删除剩余的数据透视表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Laravel,我有以下代码

Using Laravel, I have the following code

$review = Review::find(1);
$review->delete();

Review 具有使用 Product 实体定义的多对多关系.当我删除评论时,我希望它与数据透视表中的关联产品分离,但事实并非如此.当我运行上面的代码时,我仍然看到数据透视表中的链接行.

Review has a many to many relationship defined with a Product entity. When I delete a review, I'd expect it to be detached from the associated products in the pivot table, but this isn't the case. When I run the code above, I still see the linking row in the pivot table.

我在这里遗漏了什么还是这就是 Laravel 的工作方式?我知道 detach() 方法,但我认为删除实体也会自动将其与任何相关实体分离.

Have I missed something out here or is this the way Laravel works? I'm aware of the detach() method, but I thought that deleting an entity would also detach it from any related entities automatically.

Review 定义如下:

<?php
class Review extends Eloquent
{
    public function products()
    {
        return $this->belongsToMany('Product');
    }
}

Product 定义如下:

<?php
class Product extends Eloquent
{
    public function reviews()
    {
        return $this->belongsToMany('Review');
    }
}

提前致谢.

推荐答案

detach 方法用于从数据透视表中释放关系,而 delete 将删除模型记录本身,即评论表中的记录.我的理解是删除不会隐式触发分离.不过,您可以使用 模型事件 来触发数据透视表的清理,例如:

The detach method is used to release a relationship from the pivot table, whilst delete will delete the model record itself i.e. the record in the reviews table. My understanding is that delete won't trigger the detach implicitly. You can use model events to trigger a cleanup of the pivot table, though, using something like:

Review::deleting(function($review)
{
    $review->product()->detach()
}

另外,我建议这种关系是一对多的,因为一个产品会有很多评论,但一个评论不会属于很多产品(通常).

Also, I would suggest that the relationship would be a one to many, as one product would have many reviews, but one review wouldn't belong to many products (usually).

class Review extends Eloquent {
    public function product()
    {
        return $this->belongsTo('Product');
    }
}

class Product extends Eloquent {
    public function reviews()
    {
        return $this->hasMany('Review');
    }
}

当然,这需要您调整数据库结构.如果您想保留数据库结构和您当前的关系,另一种选择是在数据透视表上应用外键约束,这样当评论或产品被删除时,您可以级联删除数据透视表.

Of course, this would then require that you tweak your database structure. If you wanted to leave the database structure and your current relationships as they are, the other option would be to apply a foreign key constraint on the pivot table, such that when either a review or product is removed, you could cascade the delete on the pivot table.

// Part of a database migration
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('review_id')->references('id')->on('reviews')->onDelete('cascade');

在添加约束时,您将清理工作推送到数据库上,而不必担心在代码中处理它.

In adding the constraint, you push the cleanup work onto the database, and don't have to worry about handling it in code.

这篇关于Laravel Eloquent ORM - 多对多删除剩余的数据透视表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆