mtvs/eloquent-approval

Events not being fired if using observers

Opened this issue · 5 comments

I'm only able to listen to the events if inside the model:

// app/Model.php
protected static function boot()
{
    parent::boot();

    static::approved(function ($entity) {
        Log::debug('approved'); // it works
    });
}

It doesn't work If i'm using an observer:

// app/Observers/ModelObserver.php
public function approved(Model $model): void
{
    Log::debug('approved'); // doesn't work
}

public function saved(Model $model): void
{
    Log::debug('saved'); // doesn't work
}

public function retrivied(Model $model): void
{
    Log::debug('retrivied'); // it works
}

What could be causing this?

mtvs commented

Hey @ricardovigatti.

First, thank you for reporting this issue. It's now fixed in v1.5.0 release.

I've found the solution in the Larachat slack channel. @sisve gave me the answer (https://github.com/sisve/)

There is a need to define the $observables property at the model:

/**
 * User exposed observable events.
 * These are extra user-defined events observers may subscribe to.
 *
 * @var array
 */
protected $observables = ['approved', 'rejected'];

@mtvs , we just sent the messages almost at the same time. I'll check the new version as well.

mtvs commented

@ricardovigatti Yes, I've overwritten getObservableEvents() and added the approval events.