• Laravel Relationship Events


    Laravel Relationship Events is a package by Viacheslav Ostrovskiy that adds extra model relationship events. This package comes with the following traits that are used to register listeners on a model’s boot() method:

    • HasOneEvents
    • HasBelongsToEvents
    • HasManyEvents
    • HasBelongsToManyEvents
    • HasMorphOneEvents
    • HasMorphToEvents
    • HasMorphManyEvents
    • HasMorphToManyEvents
    • HasMorphedByManyEvents

    And from the above traits, here’s an example of a few events on a Country model that has many Users using the HasManyEventstrait:

    namespace AppModels;
    
    use AppUser;
    use CheloutRelationshipEventsConcernsHasManyEvents;
    use IlluminateDatabaseEloquentModel;
    
    class Country extends Model
    {
        use HasManyEvents;
    
        protected $fillable = [
            'name',
        ];
    
        public function users()
        {
            return $this->hasMany(User::class);
        }
    
        public static function boot()
        {
            parent::boot();
    
            static::hasManySaving(function ($parent, $related) {
                Log::info("Saving user's country {$parent->name}.");
            });
    
            static::hasManySaved(function ($parent, $related) {
                Log::info("User's country is now set to {$parent->name}.");
            });
        }
    }
    

    And the inverse of the relationship with this package might look like the following:

    namespace AppModels;
    
    use IlluminateDatabaseEloquentModel;
    use CheloutRelationshipEventsConcernsHasBelongsToEvents;
    
    class User extends Model
    {
        use HasBelongsToEvents;
    
        /**
         * Get the country associated with the user.
         */
        public function country()
        {
            return $this->belongsTo(Country::class);
        }
    
        protected static function boot()
        {
            parent::boot();
    
            static::belongsToAssociating(function ($relation, $related, $parent) {
                Log::info("Associating country {$parent->name} with user.");
            });
    
            static::belongsToAssociated(function ($relation, $related, $parent) {
                Log::info("User has been assosiated with country {$parent->name}.");
            });
        }
    }
    

    Using an overloaded associate() method, you can fire two events belongsToAssociating and belongsToAssociated:

    $country = AppModelsCountry::first();
    
    $user = factory(User::class)->create([
        'name' => 'John Smith',
    ]);
    
    // Assosiate user with country
    // This will fire belongsToAssociating and belongsToAssociated events
    $user->country()->associate($country);
    

    Learn More

    The package has documentation for each trait and association type. Check out the package on GitHub at chelout/laravel-relationship-events.


    Filed in: Laravel Packages Eloquent

  • 相关阅读:
    C#中的int、long、float、double等类型都占多少个字节的内存
    Bit 存储操作代码碎片
    unity文件写入与读取
    unity调用系统剪切板功能
    LayerMask小结
    NGUI中获取鼠标在控件内部坐标
    【Unity技巧】Unity中的优化技术
    工程源码目录
    Unity3D_NGUI_性能优化实践_CPU卡顿
    Unity3d:UI面板管理整合进ToLua
  • 原文地址:https://www.cnblogs.com/mouseleo/p/10424156.html
Copyright © 2020-2023  润新知