lorisleiva / laravel-actions

⚡️ Laravel components that take care of one specific task
https://laravelactions.com
MIT License
2.49k stars 121 forks source link

`asListener` does not trigger when registered in model class #183

Open leo-unglaub opened 2 years ago

leo-unglaub commented 2 years ago

Hey, the asListener method is not called when the action is registered in the model file itself:

// app/Models/Foobar.php
protected $dispatchesEvents = [
  'created' => MyCreatedAction::class,
  'saving' => MySavingAction::class,
];

The __construct method is called on both actions, but not the asListener or the handle method. Any ideas why? Is this a bug or a feature?

Thanks Leo

austincarpenter commented 2 years ago

That will dispatch an event with the name MyCreatedAction. Instead do something like:

protected static function booted()
{
    static::created(function (self $foobar) {
        MyCreatedAction::run($foobar);
    });
}

to simply run the action.

vpratfr commented 1 year ago

@leo-unglaub @lorisleiva This issue could be closed