protected function associateBelongsTo(array &$formatters, BelongsTo $relation, $seeding) {
        $relatedClass = get_class($relation->getRelated());
        $foreignKey = $relation->{$this->getBelongsToForeignKeyNameMethod()}();

        // Skips dynamic relationships.
        if (!isset($this->columns[$foreignKey])) {
            return;
        }

        $alwaysAssociate = $this->columns[$foreignKey]->getNotnull() || !$seeding;

        $formatters[$foreignKey] = function ($model, $insertedPKs) use ($relatedClass, $alwaysAssociate) {
            if (!isset($insertedPKs[$relatedClass])) {
                return null;
            }

            if ($alwaysAssociate) {
                return $this->generator->randomElement($insertedPKs[$relatedClass]);
            }

            return $this->generator->optional()->randomElement($insertedPKs[$relatedClass]);
        };
    }
protected function associateBelongsTo(array &$formatters, BelongsTo $relation, $seeding) {
        $relatedClass = get_class($relation->getRelated());
        $foreignKey = $relation->{$this->getBelongsToForeignKeyNameMethod()}();

        // Skips dynamic relationships.
        if (!isset($this->columns[$foreignKey])) {
            return;
        }

        $alwaysAssociate = $this->columns[$foreignKey]->getNotnull() || !$seeding;

        $formatters[$foreignKey] = function ($model, $insertedPKs) use ($relatedClass, $alwaysAssociate) {
            if (!isset($insertedPKs[$relatedClass])) {
                return null;
            }

            if ($alwaysAssociate) {
                return $this->generator->randomElement($insertedPKs[$relatedClass]);
            }

            return $this->generator->optional()->randomElement($insertedPKs[$relatedClass]);
        };
    }
protected function associateBelongsTo ( array & $ formatters , BelongsTo $ relation , $ seeding ) { $ relatedClass = get_class ( $ relation -> getRelated ( ) ) ; $ foreignKey = $ relation -> { $ this -> getBelongsToForeignKeyNameMethod ( ) } ( ) ; // Skips dynamic relationships. if ( ! isset ( $ this -> columns [ $ foreignKey ] ) ) { return ; } $ alwaysAssociate = $ this -> columns [ $ foreignKey ] -> getNotnull ( ) || ! $ seeding ; $ formatters [ $ foreignKey ] = function ( $ model , $ insertedPKs ) use ( $ relatedClass , $ alwaysAssociate ) { if ( ! isset ( $ insertedPKs [ $ relatedClass ] ) ) { return null ; } if ( $ alwaysAssociate ) { return $ this -> generator -> randomElement ( $ insertedPKs [ $ relatedClass ] ) ; } return $ this -> generator -> optional ( ) -> randomElement ( $ insertedPKs [ $ relatedClass ] ) ; } ; }
Set the closure that will be used to populate the foreign key of a Belongs To relation.

@param  array     $formatters
@param  BelongsTo $relation
@param  bool      $seeding
@return void
Set the closure that will be used to populate the foreign key of a Belongs To relation .