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

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

问题描述

使用Laravel,我有以下代码

Using Laravel, I have the following code

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

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

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.

评论定义如下:

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

产品定义如下:

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

提前感谢

推荐答案

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

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');
    }
}

当然,这将要求您调整数据库结构如果你想离开数据库结构和你的c另外一个选择是将外键约束应用到数据透视表上,这样当查看或产品被删除时,您可以在数据透视表上级联删除。

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雄辩ORM - 多对多删除数据透视表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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