chelout/laravel-relationship-events
Composer 安装命令:
composer require chelout/laravel-relationship-events
包简介
Missing relationship events for Laravel
README 文档
README
Missing relationship events for Laravel
Compatibility
| Package | Laravel | PHP |
|---|---|---|
v5.x |
13.x |
^8.3 |
v4.x |
12.x |
^8.2 |
v3.x |
11.x |
^8.2 |
v2.x |
10.x |
^8.1 |
v1.x |
6.x - 9.x |
^7.2 | ^8.0 |
Install
- Install package with composer
Stable branch:
composer require chelout/laravel-relationship-events
Development branch:
composer require chelout/laravel-relationship-events:dev-master
- Use necessary trait in your model.
Available traits:
- HasOneEvents
- HasBelongsToEvents
- HasManyEvents
- HasBelongsToManyEvents
- HasMorphOneEvents
- HasMorphToEvents
- HasMorphManyEvents
- HasMorphToManyEvents
- HasMorphedByManyEvents
use Chelout\RelationshipEvents\Concerns\HasOneEvents; use Illuminate\Database\Eloquent\Model; class User extends Model { use HasOneEvents; public static function boot() { parent::boot(); /** * One To One Relationship Events */ static::hasOneSaved(function ($parent, $related) { dump('hasOneSaved', $parent, $related); }); static::hasOneUpdated(function ($parent, $related) { dump('hasOneUpdated', $parent, $related); }); } }
use Chelout\RelationshipEvents\Concerns\HasMorphToManyEvents; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasMorphToManyEvents; public static function boot() { parent::boot(); /** * Many To Many Polymorphic Relations Events. */ static::morphToManyAttached(function ($relation, $parent, $ids, $attributes) { dump('morphToManyAttached', $relation, $parent, $ids, $attributes); }); static::morphToManyDetached(function ($relation, $parent, $ids) { dump('morphToManyDetached', $relation, $parent, $ids); }); } public function tags() { return $this->morphToMany(Tag::class, 'taggable'); } }
- Dispatchable relationship events.
It is possible to fire event classes via $dispatchesEvents properties and adding
HasDispatchableEventstrait:
use Chelout\RelationshipEvents\Concerns\HasOneEvents; use Chelout\RelationshipEvents\Traits\HasDispatchableEvents; use Illuminate\Database\Eloquent\Model; class User extends Model { use HasDispatchableEvents; use HasOneEvents; protected $dispatchesEvents = [ 'hasOneSaved' => HasOneSaved::class, ]; }
Relationships
- One To One Relations
- One To Many Relations
- Many To Many Relations
- Has Many Through Relations
- One To One Polymorphic Relations
- One To Many Polymorphic Relations
- Many To Many Polymorphic Relations
Observers
Starting from v0.4 it is possible to use relationship events in Laravel observers classes Usage is very simple. Let's take User and Profile classes from One To One Relations, add HasRelationshipObservables trait to User class. Define observer class:
namespace App\Observer; class UserObserver { /** * Handle the User "hasOneCreating" event. * * @param \App\Models\User $user * @param \Illuminate\Database\Eloquent\Model $related * * @return void */ public function hasOneCreating(User $user, Model $related) { Log::info("Creating profile for user {$related->name}."); } /** * Handle the User "hasOneCreated" event. * * @param \App\Models\User $user * @param \Illuminate\Database\Eloquent\Model $related * * @return void */ public function hasOneCreated(User $user, Model $related) { Log::info("Profile for user {$related->name} has been created."); } }
Don't forget to register an observer in the boot method of your AppServiceProvider:
namespace App\Providers; use App\Models\User; use App\Observers\UserObserver; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { // ... public function boot() { User::observe(UserObserver::class); } // ... }
And now just create profile for user:
// ... $user = factory(User::class)->create([ 'name' => 'John Smith', ]); // Create profile and assosiate it with user // This will fire two events hasOneCreating, hasOneCreated $user->profile()->create([ 'phone' => '8-800-123-45-67', 'email' => 'user@example.com', 'address' => 'One Infinite Loop Cupertino, CA 95014', ]); // ...
Todo
- Tests, tests, tests
chelout/laravel-relationship-events 适用场景与选型建议
chelout/laravel-relationship-events 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.47M 次下载、GitHub Stars 达 525, 最近一次更新时间为 2018 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「events」 「laravel」 「relations」 「relationship」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chelout/laravel-relationship-events 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chelout/laravel-relationship-events 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chelout/laravel-relationship-events 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Eloquent model updater for nested relations data for Laravel.
Missing relationship events for Laravel
Laravel package for inspecting model classes on relations en attributes.
Библиотека для реализаций паттерна Pub/Sub
Wireless Cross-Component Communication
A powerful event calendar Tool for Laravel's Nova 5.
统计信息
- 总下载量: 2.47M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 527
- 点击次数: 34
- 依赖项目数: 23
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-15