laravel-dynamic-observer maintained by waad
Description
Call observer of the model without requiring in any provider, support multi observers
Author
Last update
2026/04/05 20:08
(dev-main)
License
Downloads
147
Tags
package - utility - model - observer - laravel - laravel-php - waad-mawlood - laravel-observer - model-observer
Laravel Dynamic Observer
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.