waad / laravel-dynamic-observer
Call observer of the model without requiring in any provider, support multi observers
Package info
github.com/waadmawlood/laravel-dynamic-observer
pkg:composer/waad/laravel-dynamic-observer
Requires
- php: ^8.0
Requires (Dev)
- laravel/pint: ^1.0
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0|^10|^11
- pestphp/pest-plugin-laravel: ^1.0|^2.0|^3.0|^4.0
This package is not auto-updated.
Last update: 2026-04-05 17:42:54 UTC
README
Register model observers dynamically without service providers. Supports single and multiple observers.
Automatically connect your Laravel models to observers using traits or attributes — no service provider required.
Features
- Automatic observer registration via trait
- Custom observer support with
$observerproperty - Multiple observers support
- PHP 8.0+ attribute-based configuration
- Zero configuration needed for convention-based observers
Requirements
- PHP 8.0+
- Laravel 8.0+
Installation
composer require waad/laravel-dynamic-observer
Quick Start
Add the HasObserver trait to your model:
use Illuminate\Database\Eloquent\Model; use Waad\Observer\HasObserver; class Post extends Model { use HasObserver; }
The observer will beauto-detected based on naming convention (PostObserver in App\Observers).
Usage
1. Automatic Observer (Convention-Based)
The observer is automatically detected by naming convention:
// App\Models\Post → App\Observers\PostObserver use Waad\Observer\HasObserver; class Post extends Model { use HasObserver; }
2. Custom Observer
Specify a custom observer class:
use App\Observers\CustomObserver; use Waad\Observer\HasObserver; class Post extends Model { use HasObserver; public static $observer = CustomObserver::class; }
3. Multiple Observers
Register multiple observers:
use App\Observers\FirstObserver; use App\Observers\SecondObserver; use Waad\Observer\HasObserver; class Post extends Model { use HasObserver; public static $observer = [FirstObserver::class, SecondObserver::class]; }
4. Using Attributes (PHP 8.0+)
Use the #[HasObservers] attribute:
use App\Observers\CustomObserver; use Waad\Observer\Attributes\HasObservers; use Waad\Observer\HasObserver; #[HasObservers(CustomObserver::class)] class Post extends Model { use HasObserver; }
Note: When using the attribute, you must also use the trait
HasObserver.
Creating Observers
Generate an observer with Artisan:
php artisan make:observer PostObserver --model=Post
Observer Methods
namespace App\Observers; use App\Models\Post; class PostObserver { public function creating(Post $post) { // Called before creating } public function created(Post $post) { // Called after creating } public function updating(Post $post) { // Called before updating } public function updated(Post $post) { // Called after updating } public function saving(Post $post) { // Called before saving (create or update) } public function saved(Post $post) { // Called after saving } public function deleting(Post $post) { // Called before deleting } public function deleted(Post $post) { // Called after deleting } public function restoring(Post $post) { // Called before restoring (soft deletes) } public function restored(Post $post) { // Called after restoring } public function retrieved(Post $post) { // Called after retrieving } }
Testing
composer test
License
MIT License. See LICENSE for details.