src/Models/CmsModel.php
public function hasOne($related, $foreignKey = null, $localKey = null, $locale = null)
    {
        $relation = $this->getHasOneOrManyCaller();

        if ( ! ($specialType = $this->getCmsSpecialRelationType($relation))) {
            return parent::hasOne($related, $foreignKey, $localKey);
        }

        list($foreignKey, $fieldKey) = $this->getKeysForSpecialRelation($specialType, $foreignKey);

        $fieldId = $this->getCmsReferenceFieldId($relation);

        // create the correct hasOne, for special relations that must save field_id values
        /** @var Builder $hasOne */
        if (    $specialType === static::RELATION_TYPE_CHECKBOX
            ||  $specialType === static::RELATION_TYPE_FILE
            ||  $specialType === static::RELATION_TYPE_IMAGE
        ) {
            /** @var Model $instance */
            $instance = new $related;

            $localKey = $localKey ?: $this->getKeyName();

            $hasOne = new HasOne(
                $instance->newQuery(),
                $this,
                $instance->getTable() . '.' . $foreignKey,
                $localKey,
                $fieldId,
                $specialType
            );

        } else {

            $hasOne = parent::hasOne($related, $foreignKey, $localKey);
        }

        // add field key for wheres
        $hasOne->where($fieldKey, $fieldId);

        // limit to selected locale, if translated
        if ($this->getCmsSpecialRelationTranslated($relation)) {

            if (is_null($locale)) $locale = app()->getLocale();

            $hasOne->where(
                config('pxlcms.translatable.locale_key'),
                $this->lookUpLanguageIdForLocale($locale)
            );
        }

        return $hasOne;
    }
public function hasOne($related, $foreignKey = null, $localKey = null, $locale = null)
    {
        $relation = $this->getHasOneOrManyCaller();

        if ( ! ($specialType = $this->getCmsSpecialRelationType($relation))) {
            return parent::hasOne($related, $foreignKey, $localKey);
        }

        list($foreignKey, $fieldKey) = $this->getKeysForSpecialRelation($specialType, $foreignKey);

        $fieldId = $this->getCmsReferenceFieldId($relation);

        // create the correct hasOne, for special relations that must save field_id values
        /** @var Builder $hasOne */
        if (    $specialType === static::RELATION_TYPE_CHECKBOX
            ||  $specialType === static::RELATION_TYPE_FILE
            ||  $specialType === static::RELATION_TYPE_IMAGE
        ) {
            /** @var Model $instance */
            $instance = new $related;

            $localKey = $localKey ?: $this->getKeyName();

            $hasOne = new HasOne(
                $instance->newQuery(),
                $this,
                $instance->getTable() . '.' . $foreignKey,
                $localKey,
                $fieldId,
                $specialType
            );

        } else {

            $hasOne = parent::hasOne($related, $foreignKey, $localKey);
        }

        // add field key for wheres
        $hasOne->where($fieldKey, $fieldId);

        // limit to selected locale, if translated
        if ($this->getCmsSpecialRelationTranslated($relation)) {

            if (is_null($locale)) $locale = app()->getLocale();

            $hasOne->where(
                config('pxlcms.translatable.locale_key'),
                $this->lookUpLanguageIdForLocale($locale)
            );
        }

        return $hasOne;
    }
public function hasOne ( $ related , $ foreignKey = null , $ localKey = null , $ locale = null ) { $ relation = $ this -> getHasOneOrManyCaller ( ) ; if ( ! ( $ specialType = $ this -> getCmsSpecialRelationType ( $ relation ) ) ) { return parent :: hasOne ( $ related , $ foreignKey , $ localKey ) ; } list ( $ foreignKey , $ fieldKey ) = $ this -> getKeysForSpecialRelation ( $ specialType , $ foreignKey ) ; $ fieldId = $ this -> getCmsReferenceFieldId ( $ relation ) ; // create the correct hasOne, for special relations that must save field_id values /** @var Builder $hasOne */ if ( $ specialType === static :: RELATION_TYPE_CHECKBOX || $ specialType === static :: RELATION_TYPE_FILE || $ specialType === static :: RELATION_TYPE_IMAGE ) { /** @var Model $instance */ $ instance = new $ related ; $ localKey = $ localKey ? : $ this -> getKeyName ( ) ; $ hasOne = new HasOne ( $ instance -> newQuery ( ) , $ this , $ instance -> getTable ( ) . '.' . $ foreignKey , $ localKey , $ fieldId , $ specialType ) ; } else { $ hasOne = parent :: hasOne ( $ related , $ foreignKey , $ localKey ) ; } // add field key for wheres $ hasOne -> where ( $ fieldKey , $ fieldId ) ; // limit to selected locale, if translated if ( $ this -> getCmsSpecialRelationTranslated ( $ relation ) ) { if ( is_null ( $ locale ) ) $ locale = app ( ) -> getLocale ( ) ; $ hasOne -> where ( config ( 'pxlcms.translatable.locale_key' ) , $ this -> lookUpLanguageIdForLocale ( $ locale ) ) ; } return $ hasOne ; }
Overridden to catch special relationships to standard CMS models

@param string $related
@param string $foreignKey
@param string $localKey
@param string $locale        only used as an override, and only for ML images
@return \Illuminate\Database\Eloquent\Relations\HasOne
Overridden to catch special relationships to standard CMS models