https://github.com/randomstate/laravel-doctrine-entity-events
Easily hook in and fire native laravel events with laravel-doctrine
https://github.com/randomstate/laravel-doctrine-entity-events
Last synced: 10 months ago
JSON representation
Easily hook in and fire native laravel events with laravel-doctrine
- Host: GitHub
- URL: https://github.com/randomstate/laravel-doctrine-entity-events
- Owner: randomstate
- Created: 2018-03-02T13:40:28.000Z (over 8 years ago)
- Default Branch: develop
- Last Pushed: 2023-05-17T12:45:32.000Z (about 3 years ago)
- Last Synced: 2025-07-10T12:34:38.744Z (11 months ago)
- Language: PHP
- Size: 160 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel-Doctrine Entity Events
This package provides a simple way to hook into Doctrine2 Entity events and remap them to
native Laravel events.
## Getting Started
* Install using `composer require randomstate/laravel-doctrine-entity-events`
* Add `RandomState\LaravelDoctrineEntityEvents\LaravelDoctrineEntityEventsServiceProvider::class`
to the `providers` section of `config/app.php`
## Usage
### Configuration
**Helper Method** _(recommended)_
```php
redirect(MyEntity::class)
->postPersist(MyEntityWasCreated::class)
->postUpdate(MyEntityWasUpdated::class)
->postFlush() // falls back to default as no destination is provided
->default(SomethingHappenedToMyEntityEvent::class);
});
}
}
```
**Intercept Service Instantiation**
```php
app->resolving(EventRedirector::class, (function(EventRedirector $redirector) {
$redirector->redirect(MyEntity::class)
->postPersist(MyEntityWasCreated::class)
->postUpdate(MyEntityWasUpdated::class)
->postFlush() // falls back to default as no destination is provided
->default(SomethingHappenedToMyEntityEvent::class);
}));
}
}
```
### Events
Every Laravel Event you specify as a destination will be supplied the entity and doctrine event arguments on creation.
The entity is supplied as the first argument so you can conveniently ignore other event arguments when you are only interested
in the entity itself.
```php
redirect(MyEntity::class)
->postPersist(MyEntityWasCreated::class)
->postUpdate(function(MyEntity $entity) {
$mailer = app('mailer');
event(new MyEntityWasUpdated($entity, $mailer)); // customised instantiation
})
->postFlush() // falls back to default as no destination is provided
->default(SomethingHappenedToMyEntityEvent::class);
});
```