简体   繁体   English

删除帖子时如何删除laravel中的所有相关评论,喜欢和通知

[英]When deleting the post How to delete all related comments , likes and notifications in laravel

I want to delete all related data when I delete a post. 我想删除帖子时删除所有相关数据。 All comments likes and all notifications related to this post are also deleted when I delete this post. 当我删除此帖子时,所有喜欢的评论和与此帖子相关的所有通知也会被删除。 How can I do this ? 我怎样才能做到这一点 ? I do that when a user delets all his posts, buckets, comments, and reported users is also deleted. 当用户删除其所有帖子,存储桶,评论和报告的用户时,我也会这样做。 Here is the Code of this. 这是此代码。 But i also want to delete all the things related to the post like notifications, likes etc. Please help. 但我也想删除与该帖子相关的所有内容,例如通知,喜欢等。请帮助。

public function destroy($id)
{
    $user = User::find($id);

    $user->posts()->forceDelete();
    $user->buckets()->forceDelete();
    $user->comments()->forceDelete();
    $user->reportedUser()->forceDelete();

    $user->forceDelete();

    return redirect()->route('users.index')->with('Success','User Deleted Successfully');
}

This code delete all the things that relates to user when user is deleted but i also want to delete all things related to post. 这段代码删除了删除用户时与用户有关的所有东西,但我也想删除与帖子有关的所有东西。 How i can do this? 我该怎么做?

You can model events to trigger off an action once the Post record is being deleted. 您可以对事件建模,以在删除过Post记录Post触发操作。 In your Post model, you can add a $dispatchesEvents property: 在您的Post模型中,您可以添加$dispatchesEvents属性:

protected $dispatchesEvents = [
    'deleting' => \App\Events\PostDeleting::class,
];

After this, you can create the new event: 之后,您可以创建新事件:

php artisan make:event PostDeleting

A new event file should be added ( app\\events\\PostDeleting.php ). 应该添加一个新的事件文件( app\\events\\PostDeleting.php )。

Here you can define the data which will be passed to the event listener. 在这里,您可以定义将传递给事件侦听器的数据。

<?php

namespace App\Events;

use App\Post;
use Illuminate\Queue\SerializesModels;

class PostDeleting
{
    use SerializesModels;

    public $post;

    /**
     * Create a new event instance.
     *
     * @param \App\Post $post
     */
    public function __construct(Post $post)
    {
        $this->post = $post;
    }
}

After this, you will need to create the event listener (which will listen out for the post being deleted): 之后,您将需要创建事件侦听器 (它将侦听要删除的帖子):

php artisan event:listener PostDeleting

This will create a listener ( app/listeners/PostDeleting.php ). 这将创建一个侦听器( app/listeners/PostDeleting.php )。 Here, you define the functionality which is ran once the Post is deleting (this uses the variable defined in the event : 在这里,您定义了删除Post立即运行的功能(它使用在event中定义的变量:

<?php

namespace App\Listeners;

use App\Events\PostDeleting as PostDeletingEvent;

class PostDeleting
{
    /**
     * Handle the event.
     *
     * @param  \App\Events\PostDeleting $event
     * @return mixed
     */
    public function handle(PostDeletingEvent $event)
    {
        $event->user->likes()->delete(); // or whatever relation you need to delete.
    }
}

You can set in your migrations 您可以设置迁移

Eg comments migration 例如comments迁移

$table->foreign('post_id')
      ->references('id')
      ->on('posts')
      ->onDelete('cascade');

No need to write any other logic. 无需编写任何其他逻辑。 you can write something like that on your relational migration 您可以在关系迁移中编写类似的内容

For polymorphic relationship Eg 对于多态关系,例如

Post Model

protected static function boot()
{
    parent::boot();

    static::deleting(function ($post) {
        $post->comments()->delete();
        // ...
    });

}

or You can manage by Cascade polymorphic delete package 或者您可以通过Cascade多态删除程序包进行管理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM