zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    ES5 ES6 作用域声明部分
    js 内建函数reduce
    $apply的使用与否
    得分-星星
    CSS3中translate、transform和translation的区别和联系
    vue 学习笔记
    -webkit-line-clamp 多行文字溢出...
    八位二进制数为什么表示范围(-128~~+127)理解
    vs2017_enterprise正式版离线安装包bt下载
    RSA密钥之C#格式与Java格式转换
  • 原文地址:https://www.cnblogs.com/mouseleo/p/10424156.html
Copyright © 2011-2022 走看看