thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

Delete event not firing when User is deleted from Voyager admin panel #5504

Closed moonvader closed 2 years ago

moonvader commented 2 years ago

Laravel version

6.17

PHP version

7.4

Voyager version

1.5.2

Database

MySQL

Description

In User model I want to run custom actions when user is deleted so I add this events using code below

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

        static::deleted(function ($model) {
           someAction1();
        });

        static::saved(function ($model) {
            someAction2();
        });
    }

When I delete user from Voyager this actions are not being called. What am I doing wrong and are the any other way to run custom action when user is deleted from voyager admin panel?

Steps to reproduce

Delete user from Voyager admin panel

Expected behavior

deleted or deleting events are called

Screenshots

No response

Additional context

No response

emptynick commented 2 years ago

Voyager uses destroy to delete multiple model instances at once. This doesn't trigger the deleted event because the actual model is not retreived before. But Voyager fires BreadDataDeleted

moonvader commented 2 years ago

Thank you, in this case I need to add my custom listener? how can I access deleted user's id from it?

howdu commented 2 years ago

Add this into service provider e.g EventServiceProvider Event::listen(\TCG\Voyager\Events\BreadDataDeleted::class, CustomDeleteListener::class);

Then create a listener class

<?php

use TCG\Voyager\Events\BreadDataDeleted;

class CustomDeleteListener
{
    /**
     * Handle the event.
     *
     * @param  BreadDataDeleted  $event
     * @return void
     */
    public function handle(BreadDataDeleted $event)
    {
        if (!empty($event->dataType)) {
            switch ($event->dataType->slug) {
                case 'users':
                    $request = app(Request::class);

                    // custom logic

                    break;
            }
        }
    }
}

More info on events: https://laravel.com/docs/8.x/events

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.