laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

Ability to get event name which fires in Eloquent booted method #2656

Open foremtehan opened 3 years ago

foremtehan commented 3 years ago

Looking for a way to get event name which fires in booted method in string.

Instead of :

protected static function booted()
{
    static::updating(function (self $m) {
        //same thing
    });

    static::updated(function (self $m) {
        //same thing
    });
}

I wish i could able to do this:

if (in_array($event = static::getEventName(), ['updating', 'updated'])) {
    static::$event(function (self $m) {
        //
    });
}
rs-sliske commented 3 years ago

this package is out of date but you can do like this https://github.com/RMoorePHP/change-recorder/blob/4dac6bc5771507e4f1bd6277d2a99d84da555275/src/RecordsChanges.php#L9-L13