model 中 $hidden 的属性如何在 ORM eager load 时设置可见 
                            
                                                    
                        
                    
                    
  
                    
                    目前的做法是子类重写父类Model的 relationsToArray()方法;
有没有更好的方式
public function relationsToArray()
    {
        $attributes = [];
        foreach ($this->getArrayableRelations() as $key => $value) {
            // If the values implements the Arrayable interface we can just call this
            // toArray method on the instances which will convert both models and
            // collections to their proper array form and we'll set the values.
            if ($value instanceof Arrayable) {
//-----------------------------------增加的部分-----------------------------------------------------
                if($value->getTable() == 'childModelName'){
                    $value = $value->makeVisible('hiddenAttributeName');
                }
//-----------------------------------------------------------------------------------------------------
                $relation = $value->toArray();
            }
            // If the value is null, we'll still go ahead and set it in this list of
            // attributes since null is used to represent empty relationships if
            // if it a has one or belongs to type relationships on the models.
            elseif (is_null($value)) {
                $relation = $value;
            }
            // If the relationships snake-casing is enabled, we will snake case this
            // key so that the relation attribute is snake cased in this returned
            // array to the developers, making this consistent with attributes.
            if (static::$snakeAttributes) {
                $key = Str::snake($key);
            }
            // If the relation value has been set, we will set it on this attributes
            // list for returning. If it was not arrayable or null, we'll not set
            // the value on the array because it is some type of invalid value.
            if (isset($relation) || is_null($value)) {
                $attributes[$key] = $relation;
            }
            unset($relation);
        }
        return $attributes;
    }
                        
                        
                                            
          
                    
                    
          
          
                关于 LearnKu
              
                    
                    
                    
 
推荐文章: