protected function prepareAttributes($attributes)
    {
        foreach($this->getAttributeConfig() as $attr => $config){

            // If the attribute is not defined in model or $attributes array
            // set the default value.
            if(!isset($this->attributes[$attr]) && !isset($attributes[$attr])){

                $attributes[$attr] = $config['default'];

            }

        }

        foreach($attributes as $key => $value){

            if(isset($this->config['attributes'][$key])){

                $attributes[$key] = $this->$key()->before_save($value);

            } elseif(isset($this->config['relationships'][$key])){

                unset($attributes[$key]);

                $relationship = $this->config['relationships'][$key];

                $this->translateShorthand($value);

                switch($relationship['type']){

                    case Relationship::HAS_PIVOT:

                        if($value instanceof Collection) $value = $value->all();

                        if($value instanceof EntityContract) $value = [$value];

                        $this->$key()->saveMany($value);

                        break;

                    case Relationship::HAS_MANY:

                        $this->$key()->saveMany((array)$value);

                        break;

                    case Relationship::BELONGS_TO:

                        $this->$key()->associate($value);

                        break;

                    case Relationship::HAS_ONE:

                        $this->$key()->save($value);

                        break;

                    default:

                        throw new \Exception(sprintf(
                            "Unknown relationship type \"%s\" on entity \"%s\"",
                            $relationship['type'],
                            $relationship['entity']
                        ));

                        break;
                }

            }

        }

        return $attributes;
    }
protected function prepareAttributes($attributes)
    {
        foreach($this->getAttributeConfig() as $attr => $config){

            // If the attribute is not defined in model or $attributes array
            // set the default value.
            if(!isset($this->attributes[$attr]) && !isset($attributes[$attr])){

                $attributes[$attr] = $config['default'];

            }

        }

        foreach($attributes as $key => $value){

            if(isset($this->config['attributes'][$key])){

                $attributes[$key] = $this->$key()->before_save($value);

            } elseif(isset($this->config['relationships'][$key])){

                unset($attributes[$key]);

                $relationship = $this->config['relationships'][$key];

                $this->translateShorthand($value);

                switch($relationship['type']){

                    case Relationship::HAS_PIVOT:

                        if($value instanceof Collection) $value = $value->all();

                        if($value instanceof EntityContract) $value = [$value];

                        $this->$key()->saveMany($value);

                        break;

                    case Relationship::HAS_MANY:

                        $this->$key()->saveMany((array)$value);

                        break;

                    case Relationship::BELONGS_TO:

                        $this->$key()->associate($value);

                        break;

                    case Relationship::HAS_ONE:

                        $this->$key()->save($value);

                        break;

                    default:

                        throw new \Exception(sprintf(
                            "Unknown relationship type \"%s\" on entity \"%s\"",
                            $relationship['type'],
                            $relationship['entity']
                        ));

                        break;
                }

            }

        }

        return $attributes;
    }
protected function prepareAttributes ( $ attributes ) { foreach ( $ this -> getAttributeConfig ( ) as $ attr => $ config ) { // If the attribute is not defined in model or $attributes array // set the default value. if ( ! isset ( $ this -> attributes [ $ attr ] ) && ! isset ( $ attributes [ $ attr ] ) ) { $ attributes [ $ attr ] = $ config [ 'default' ] ; } } foreach ( $ attributes as $ key => $ value ) { if ( isset ( $ this -> config [ 'attributes' ] [ $ key ] ) ) { $ attributes [ $ key ] = $ this -> $ key ( ) -> before_save ( $ value ) ; } elseif ( isset ( $ this -> config [ 'relationships' ] [ $ key ] ) ) { unset ( $ attributes [ $ key ] ) ; $ relationship = $ this -> config [ 'relationships' ] [ $ key ] ; $ this -> translateShorthand ( $ value ) ; switch ( $ relationship [ 'type' ] ) { case Relationship :: HAS_PIVOT : if ( $ value instanceof Collection ) $ value = $ value -> all ( ) ; if ( $ value instanceof EntityContract ) $ value = [ $ value ] ; $ this -> $ key ( ) -> saveMany ( $ value ) ; break ; case Relationship :: HAS_MANY : $ this -> $ key ( ) -> saveMany ( ( array ) $ value ) ; break ; case Relationship :: BELONGS_TO : $ this -> $ key ( ) -> associate ( $ value ) ; break ; case Relationship :: HAS_ONE : $ this -> $ key ( ) -> save ( $ value ) ; break ; default : throw new \ Exception ( sprintf ( "Unknown relationship type \"%s\" on entity \"%s\"" , $ relationship [ 'type' ] , $ relationship [ 'entity' ] ) ) ; break ; } } } return $ attributes ; }
Save various model relationships; used when creating a new entity
todo move to relationship model
@param array $attributes
@return array
@throws \Exception Unknown relationship type
Save various model relationships ; used when creating a new entity todo move to relationship model