Php 删除laravel中的行时删除所有关系
我有帖子、评论和通知表 每个帖子都有很多评论 每个评论都有许多通知Php 删除laravel中的行时删除所有关系,php,laravel,eloquent,eloquent-relationship,Php,Laravel,Eloquent,Eloquent Relationship,我有帖子、评论和通知表 每个帖子都有很多评论 每个评论都有许多通知 class Post extends Model { public function notifications() { return $this->morphOne(Notification::class, 'to'); } public function comments() { return $this->hasMany(Comment::class,
class Post extends Model
{
public function notifications() {
return $this->morphOne(Notification::class, 'to');
}
public function comments() {
return $this->hasMany(Comment::class, 'post_id');
}
public static function boot() {
parent::boot();
static::deleting(function($post) {
$post->comments()->delete();
$post->notifications()->delete();
});
}
}
每个帖子都有很多通知
class Post extends Model
{
public function notifications() {
return $this->morphOne(Notification::class, 'to');
}
public function comments() {
return $this->hasMany(Comment::class, 'post_id');
}
public static function boot() {
parent::boot();
static::deleting(function($post) {
$post->comments()->delete();
$post->notifications()->delete();
});
}
}
当我删除帖子时,我应该同时删除通知和评论,
但问题是当我删除评论时,通知不会随之删除,
当我直接删除评论时,它们会被删除,但当我删除帖子时,我需要删除评论通知 链接将不起作用,因为模型未初始化。
最好的解决方案是循环并删除单个注释
static::deleting(function($post) {
foreach ($post->comments() as $comment){
$comment->delete();
}
});
Laravel不会实例化它删除的相关模型,这就是为什么直接删除评论时会删除通知,而通过删除帖子删除评论时不会删除通知。在删除帖子时,您必须实例化注释才能使其正常工作
class Post extends Model {
public function notifications() {
return $this->morphOne(Notification::class, 'to');
}
public function comments() {
return $this->hasMany(Comment::class, 'post_id');
}
public static function boot() {
parent::boot();
static::deleting(function($post) {
// here you could instantiate each related Comment
// in this way the boot function in the Comment model will be called
$post->comments->each(function($comment) {
// and then the static::deleting method when you delete each one
$comment->delete();
});
$post->notifications()->delete();
});
}
}
为了记录在案,我在评论中添加了我们讨论的内容,因为它可以服务于遇到相同问题的其他人,并且在评论中它可以不被注意。功劳归于OP@Mahmoud Ben Jabir 但是如果帖子有100条评论,它将执行100个查询来删除它们!我将找出如何用最少的查询删除
我已经在评论上有了onDelete,但是通知是多态的,所以它对它们不起作用 我将使用的解决方案是:
1-获取与帖子相关的评论ID。
2-从通知中删除类型为注释和id的通知(id)。
3-删除与帖子相关的评论。
4-删除与帖子相关的通知
5-删除帖子 请使用(假设帖子和评论之间存在关系)
在迁移文件中建立外部关系。因此,当你删除相关帖子时,它的评论也会被删除。你能展示一下你如何删除帖子的代码吗?Laravel没有实例化它删除的相关模型,这就是为什么当你直接删除评论时,通知会被删除,而当通过删除帖子删除评论时,通知不会被删除。你必须在删除一篇文章时实例化评论,使其正常工作,但如果该文章有100条评论,它将执行100个查询来删除它们!我会找出如何用最少的查询来删除,Thanks@MahmoudBenJabir是的,你说得对。如果在DB级别上使用onDelete('cascade'),我认为更有效的方法是使用onDelete('cascade'),但只适用于关系数据库和在DB级别上配置外键我已经在注释上使用了onDelete,但通知是多态的,因此无法使用它们,我想出了一种方法来获取评论ID,然后手动删除通知,而不是依赖雄辩的关系。。。感谢man的讨论我将使用的解决方案是:1-获取与帖子相关的评论ID。2-从通知中删除,其中类型为(ids)中的注释和id。3-删除与成本相关的注释。4-删除与帖子相关的通知5-删除帖子。有什么建议吗?是的,这和我使用的登录名是一样的,我使用了第二个登录名,只是用这种方式删除了评论,$post->comments()->deleted();我说的是评论通知!这也可以在通知和评论方面进行。
class Post extends Model {
public function notifications() {
return $this->morphOne(Notification::class, 'to');
}
public function comments() {
return $this->hasMany(Comment::class, 'post_id');
}
public static function boot() {
parent::boot();
static::deleting(function($post) {
// here you could instantiate each related Comment
// in this way the boot function in the Comment model will be called
$post->comments->each(function($comment) {
// and then the static::deleting method when you delete each one
$comment->delete();
});
$post->notifications()->delete();
});
}
}
public static function boot() {
parent::boot();
static::deleting(function($post) {
// 1- Get Ids of Comments that are related to the Post.
$ids = $post->comments()->pluck('id');
// 2- Delete from Notifications where type IS Comment AND id in (ids).
Notification::where('entity_type', 'App\Comment')->whereIn('entity_id', $ids)->delete();
// 3- Delete Comments related to the Post.
$post->comments()->delete();
// 4- Delete The Notifications Related to the Post
$post->notifications()->delete();
});
// 5- Delete The Post.
}
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade');