Download the PHP package plank/before-and-after-model-events without Composer
On this page you can find all versions of the php package plank/before-and-after-model-events. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download plank/before-and-after-model-events
More information about plank/before-and-after-model-events
Files in plank/before-and-after-model-events
Package before-and-after-model-events
Short Description Add before and after events to each existing Laravel Model event.
License MIT
Homepage https://github.com/plank/before-and-after-model-events
Informations about the package before-and-after-model-events
Before and After Model Events
This package adds before and after events for every Laravel Eloquent model event, giving you complete control over your model's lifecycle. It works with all standard Laravel model events (creating, created, updating, updated, deleting, deleted, etc.) and any custom events you define.
Features
- ๐ Zero Configuration - Just add the trait and start using before/after events
- ๐ฏ Works with ALL Events - Standard Laravel events AND custom events
- ๐ Event Prevention - Before events can prevent the main event from firing
- ๐๏ธ Clean API - Static methods for registering event listeners with full IDE support
- ๐งช Fully Tested - Comprehensive test suite with 22 tests and 68 assertions
- โก Performance Focused - Minimal overhead with dynamic event registration
- ๐ง Laravel Integration - Works seamlessly with existing Laravel event systems
Event Flow
When working with model events, this package ensures the following execution order:
For standard Laravel events (like saving a model):
beforeCreatingโcreatingโafterCreatingbeforeSavingโsavingโafterSavingbeforeCreatedโcreatedโafterCreatedbeforeSavedโsavedโafterSaved
For custom events (like publishing a post):
beforePublishingโpublishingโafterPublishing
Installation
You can install the package via composer:
Usage
Simply add the BeforeAndAfterEvents trait to any Eloquent model:
Basic Usage
1. Add the Trait
Add the BeforeAndAfterEvents trait to any Eloquent model:
2. Register Event Listeners
Use the static methods to register before and after event listeners:
Advanced Features
Event Prevention
Before events can prevent the main event (and subsequent after events) from firing by returning false:
Custom Events
The package works seamlessly with any custom events you fire on your models:
Multiple Listeners
You can register multiple listeners for the same event:
Soft Deletes Support
The package works perfectly with Laravel's soft deletes:
Integration with Existing Event Systems
The package works alongside existing Laravel event dispatchers and observers:
Event Reference
Standard Laravel Events
All standard Laravel model events are supported:
retrieved- Before/after model is retrieved from databasecreating- Before/after model is being createdcreated- Before/after model has been createdupdating- Before/after model is being updatedupdated- Before/after model has been updatedsaving- Before/after model is being saved (create or update)saved- Before/after model has been saveddeleting- Before/after model is being deleteddeleted- Before/after model has been deletedrestoring- Before/after soft-deleted model is being restoredrestored- Before/after soft-deleted model has been restoredreplicating- Before/after model is being replicatedforceDeleting- Before/after model is being force deletedforceDeleted- Before/after model has been force deleted
Custom Events
Any event name can be used - the package will automatically register the before/after variants:
How It Works
The package uses a simple but powerful approach:
-
Dynamic Event Registration: When you call
beforeEvent()orafterEvent(), the package registers the base event (e.g.,publishing) in a static registry and adds the before/after variants (beforePublishing,afterPublishing) to the model's observable events. -
Event Interception: The trait overrides the
fireModelEvent()method to intercept all model events and fire the before/after events at the appropriate times. - Minimal Overhead: Events are only registered when actually used, and the trait adds minimal performance overhead to your models.
Requirements
- PHP 8.3 or higher
- Laravel 10.0, 11.0, or 12.0
Testing
The package includes a comprehensive test suite with 22 tests covering:
- Standard Laravel events (creating, updating, deleting, etc.)
- Custom events and dynamic registration
- Event prevention and flow control
- Soft deletes and restoration
- Multiple listeners per event
- Edge cases and error handling
- Integration with existing Laravel event systems
Run the tests:
Run tests with coverage:
Run static analysis:
Credits
- Kurt Friars
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
Security Vulnerabilities
If you discover a security vulnerability within Before and After Model Events, please send an e-mail to [email protected]. All security vulnerabilities will be promptly addressed.
Check Us Out!
Plank focuses on impactful solutions that deliver engaging experiences to our clients and their users. We're committed to innovation, inclusivity, and sustainability in the digital space. Learn more about our mission to improve the web.