alexstewartja/laravel-attribute-observer
Composer 安装命令:
composer require alexstewartja/laravel-attribute-observer
包简介
Observe (and react to) attribute changes made on Eloquent models.
README 文档
README
A Laravel package which allows you to observe (and react to) attribute changes made on Eloquent models.
Installation
You can install the package via composer:
composer require alexstewartja/laravel-attribute-observer
Configuration
Publish the config file (config/attribute-observer.php) with:
php artisan vendor:publish --provider="AlexStewartJA\LaravelAttributeObserver\LaravelAttributeObserverServiceProvider"
This is the default content of the published config file:
return [ /* |-------------------------------------------------------------------------- | Attribute Observers |-------------------------------------------------------------------------- | | Here you may configure all desired Models and their respective Attribute | Observers. For example: | | 'observers' => [ | \App\Models\Order::class => [ | \App\AttributeObservers\OrderStatusObserver::class, | ], | ] | */ 'observers' => [ // Define your model & attribute observers here... ] ];
Populate the observers array with your desired Model => Attribute Observer mappings.
Usage
Attribute Observers
The make:laravel-attribute-observer Artisan command is the easiest way to create a new attribute observer class:
php artisan make:laravel-attribute-observer OrderStatusObserver --model=Order
This command will place the new attribute observer in your App/AttributeObservers directory. If this directory does not exist,
Artisan will create it for you. Your freshly generated attribute observer will look like the following:
<?php namespace App\AttributeObservers; use App\Models\Order; class OrderStatusObserver { /** * Handle changes to the "id" field of Order on "created" events. * * @param \App\Models\Order $order * @param mixed $newValue The current value of the field * @param mixed $oldValue The previous value of the field * @return void */ public function onIdCreated(Order $order, mixed $newValue, mixed $oldValue) { // } }
You may then modify the code to match your business logic, for example:
<?php namespace App\AttributeObservers; use App\Events\OrderStatusChanged; use App\Models\Order; class OrderStatusObserver { /** * Handle changes to the "status" field of Order on "saved" events. * * @param \App\Models\Order $order * @param mixed $newValue The current value of the field * @param mixed $oldValue The previous value of the field * @return void */ public function onStatusSaved(Order $order, mixed $newValue, mixed $oldValue) { // Dispatch an event that sends an order update email to the customer OrderStatusChanged::dispatch($order); } }
Attribute Observer methods are always supplied with the model instance, the new attribute value and the previous attribute value, in that order.
Events
You may observe all the typical CRUD events dispatched by Eloquent models during their lifecycles. Supported events are:
creating, created, updating, updated, saving, saved, deleting, deleted.
The naming convention to follow when defining attribute observer methods is: on|AttributeName|Event
So, let's say we want to check a user's email against a global spam/vanity mail list each time they attempt to update it,
we would implement that logic in an attribute observer method called onEmailUpdating.
Note that the attribute name must be in PascalCase and the event name must be Capitalized.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
Donations
I maintain this package in my spare time. If it's beneficial to you, consider buying me a coffee to keep it improving.
alexstewartja/laravel-attribute-observer 适用场景与选型建议
alexstewartja/laravel-attribute-observer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 86.37k 次下载、GitHub Stars 达 72, 最近一次更新时间为 2021 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「alexstewartja」 「laravel-attribute-observer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alexstewartja/laravel-attribute-observer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alexstewartja/laravel-attribute-observer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alexstewartja/laravel-attribute-observer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
Turn any PDF into a Pingen-ready A4 letter (generated address cover page + A4 normalisation + safe margins) and send it through the Pingen print & mail API. Laravel-first.
统计信息
- 总下载量: 86.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 73
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-30