haganjones/laravel-observe-properties
Composer 安装命令:
composer require haganjones/laravel-observe-properties
包简介
A simple trait to allow you to observe changing properties on a Model.
README 文档
README
A simple trait to allow you to observe changing properties on a Model.
When observing a model it is sometimes necessary to check to see if a particular property on a model has changed, then perform an action. If you need to do this a lot then your observer methods can soon become clogged up, hopefully this trait will help you out!
Install via Composer
composer require "haganjones/laravel-observe-properties"
To Use
Include the trait in your Observation Classes:
<?php namespace App\Observers; use HaganJones\LaravelObserveProperties\ObserveProperties; class UserObserver { use ObserveProperties; }
Including this trait in your class allows you to make use of the camelcaseProperty + Eventname methods.
For example, following on from our UserObserver class above:
// Inside UserObserver Class public function emailUpdated(User $user, $oldValue, $newValue) { $format = 'User %d changed their email from %s to %s'; Log::info(sprintf($format, $user->id, $oldValue, $newValue)); // User 1 changed their email from oldrusty@example.org to newshiny@example.org }
Available Events
All default Laravel Events are supported, please not some events will only receive one value. See below for full list of events and arguments.
| Method | Arguments Received |
|---|---|
| yourPropertyCreating | (Model $yourModel, $value) |
| yourPropertyCreated | (Model $yourModel, $value) |
| yourPropertyUpdating | (Model $yourModel, $oldValue, $newValue) |
| yourPropertyUpdated | (Model $yourModel, $oldValue, $newValue) |
| yourPropertySaving | (Model $yourModel, $oldValue, $newValue) |
| yourPropertySaved | (Model $yourModel, $oldValue, $newValue) |
| yourPropertyDeleting | (Model $yourModel, $value) |
| yourPropertyDeleted | (Model $yourModel, $value) |
| yourPropertyRestoring | (Model $yourModel, $value) |
| yourPropertyRestored | (Model $yourModel, $value) |
Caveats
As per the Laravel docs the Saving|Saved events fire along
side Creating|Created and Updating|Updated methods.
With this in mind when creating a Model the Saving|Saved event will receive
both $oldValue and $newValue arguments but they will have the
same value.
Using in existing Observer classes.
It is possible you would want to add this functionality inside your existing Observer classes in this case you will need to include the trait but give it's methods some aliases e.g
<?php namespace App\Observers; use HaganJones\LaravelObserveProperties\ObserveProperties; use App\User; class UserObserver { use ObserveProperties { updating as updatingProperties; //add in other method aliases here. } public function updating(User $user) { //Do things with $user return $this->updatingProperties($user); } public function firstNameUpdating(User $user, $oldValue, $newValue) { //$user->first_name was just updated. } }
haganjones/laravel-observe-properties 适用场景与选型建议
haganjones/laravel-observe-properties 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 373 次下载、GitHub Stars 达 9, 最近一次更新时间为 2017 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「trait」 「laravel」 「observe」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 haganjones/laravel-observe-properties 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 haganjones/laravel-observe-properties 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 haganjones/laravel-observe-properties 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Let a service provider handle your model events
Trait providing methods to set class properties with an array.
personal used for Roketin Engine. Keep a change history for your models using laravel version 5.*
Expansion traits for Yii2 components
Traits to build collections of specific objects
KISS implementation of RequestTrait with usage of CURL
统计信息
- 总下载量: 373
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-15