承接 kettasoft/booter 相关项目开发

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

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

kettasoft/booter

最新稳定版本:v1.0.2

Composer 安装命令:

composer require kettasoft/booter

包简介

The Laravel Model Event Booter package provides a flexible way to organize and manage model event handling in Laravel applications. It allows developers to define specific actions for model events (such as created, updated, deleted, etc.) by associating them with custom classes. The package simplifi

README 文档

README

Laravel Model Event Booter is a package that simplifies managing model events in Laravel by allowing you to define and map model events (e.g., created, updated, deleted, etc.) to custom classes. These classes handle the logic associated with those events, making your code cleaner, modular, and easier to maintain.

Sponsor Latest Stable Version Total Downloads License tests

Features

  • Automatically boot and trigger model events with custom logic.
  • Organize event-specific logic into separate, reusable classes.
  • Works with Laravel's built-in model events (created, updated, deleted, etc.).
  • Easy-to-use HasBooter trait that handles event firing.

Installation

    • Install the package via Composer:
composer require kettasoft/booter
    • Add the HasBooter trait to any model where you want to manage events.
    • Define the $events array in the model to map events to the classes that will handle them.
    • Publish the package's configuration file by running:
php artisan vendor:publish --provider="Scaffolding\Booter\Providers\BooterServiceProvider" --tag=config

Usage

    • Add the HasBooter Trait to Your Model

    • Use the HasBooter trait in your model to enable event handling:

use Scaffolding\Booter\Traits\HasBooter;

class Post extends Model
{
    use HasBooter;

    /**
     * The event-to-class mappings.
     *
     * @var array
     */
    protected static $events = [
        'created' => [
            \App\Boot\AttachAuthorIdBoot::class,
        ],
        'updated' => [
            \App\Boot\LogChangesBoot::class,
        ],
    ];
}
    • Create Event Handler Classes Create a class for each event that you want to handle. Each class should have a handle method where you define the logic to run when the event is triggered.
namespace App\Boots;

use Scaffolding\Booter\HasBooter;

class AttachAuthorIdBoot extends HasBooter
{
    /**
     * Handle the model event.
     *
     * @param  \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function handle(\Illuminate\Database\Eloquent\Model $model)
    {
        // Custom logic for 'created' event
        $model->author_id = auth()->id();
        $model->save();
    }
}

The class defined in the event will be called automatically when the event occurs.

    • Handle Multiple Events

You can define multiple events in the $events array for a single model. Each event can have one or more classes that will be triggered in sequence.

protected static $events = [
    'created' => [
        \App\Boot\AttachAuthorIdBoot::class,
        \App\Boot\SendNotificationBoot::class,
    ],
    'updated' => [
        \App\Boot\LogChangesBoot::class,
    ],
];
    • Event Handling Flow

    • When the model event (like created, updated, etc.) is triggered, the package automatically fires the associated class.

    • Each class must have a handle method where you implement the custom logic.

Contributing

Contributions are welcome! If you find any issues or have ideas for improvements, feel free to submit a pull request or open an issue.

License

This package is open-source software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-01