承接 waad/laravel-dynamic-observer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

waad/laravel-dynamic-observer

最新稳定版本:v2.0.0

Composer 安装命令:

composer require waad/laravel-dynamic-observer

包简介

Call observer of the model without requiring in any provider, support multi observers

README 文档

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 $observer property
  • 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.

统计信息

  • 总下载量: 145
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固