src/Execution/MutationExecutor.php
protected static function saveModelWithPotentialParent(Model $model, Collection $args, ?Relation $parentRelation = null): Model
    {
        [$belongsTo, $remaining] = self::partitionArgsByRelationType(
            new ReflectionClass($model),
            $args,
            BelongsTo::class
        );

        // Use all the remaining attributes and fill the model
        $model->fill(
            $remaining->all()
        );

        $belongsTo->each(function (array $nestedOperations, string $relationName) use ($model): void {
            /** @var \Illuminate\Database\Eloquent\Relations\BelongsTo $relation */
            $relation = $model->{$relationName}();

            if ($create = $nestedOperations['create'] ?? false) {
                $belongsToModel = self::executeCreate(
                    $relation->getModel()->newInstance(),
                    new Collection($create)
                );
                $relation->associate($belongsToModel);
            }

            if ($connect = $nestedOperations['connect'] ?? false) {
                // Inverse can be hasOne or hasMany
                /** @var \Illuminate\Database\Eloquent\Relations\BelongsTo $belongsTo */
                $belongsTo = $model->{$relationName}();
                $belongsTo->associate($connect);
            }

            if ($update = $nestedOperations['update'] ?? false) {
                $belongsToModel = self::executeUpdate(
                    $relation->getModel()->newInstance(),
                    new Collection($update)
                );
                $relation->associate($belongsToModel);
            }

            // We proceed with disconnecting/deleting only if the given $values is truthy.
            // There is no other information to be passed when issuing those operations,
            // but GraphQL forces us to pass some value. It would be unintuitive for
            // the end user if the given value had no effect on the execution.
            if ($nestedOperations['disconnect'] ?? false) {
                $relation->dissociate();
            }

            if ($nestedOperations['delete'] ?? false) {
                $relation->delete();
            }
        });

        if ($parentRelation && ! $parentRelation instanceof BelongsToMany) {
            // If we are already resolving a nested create, we might
            // already have an instance of the parent relation available.
            // In that case, use it to set the current model as a child.
            $parentRelation->save($model);

            return $model;
        }

        $model->save();

        if ($parentRelation instanceof BelongsToMany) {
            $parentRelation->syncWithoutDetaching($model);
        }

        return $model;
    }
protected static function saveModelWithPotentialParent(Model $model, Collection $args, ?Relation $parentRelation = null): Model
    {
        [$belongsTo, $remaining] = self::partitionArgsByRelationType(
            new ReflectionClass($model),
            $args,
            BelongsTo::class
        );

        // Use all the remaining attributes and fill the model
        $model->fill(
            $remaining->all()
        );

        $belongsTo->each(function (array $nestedOperations, string $relationName) use ($model): void {
            /** @var \Illuminate\Database\Eloquent\Relations\BelongsTo $relation */
            $relation = $model->{$relationName}();

            if ($create = $nestedOperations['create'] ?? false) {
                $belongsToModel = self::executeCreate(
                    $relation->getModel()->newInstance(),
                    new Collection($create)
                );
                $relation->associate($belongsToModel);
            }

            if ($connect = $nestedOperations['connect'] ?? false) {
                // Inverse can be hasOne or hasMany
                /** @var \Illuminate\Database\Eloquent\Relations\BelongsTo $belongsTo */
                $belongsTo = $model->{$relationName}();
                $belongsTo->associate($connect);
            }

            if ($update = $nestedOperations['update'] ?? false) {
                $belongsToModel = self::executeUpdate(
                    $relation->getModel()->newInstance(),
                    new Collection($update)
                );
                $relation->associate($belongsToModel);
            }

            // We proceed with disconnecting/deleting only if the given $values is truthy.
            // There is no other information to be passed when issuing those operations,
            // but GraphQL forces us to pass some value. It would be unintuitive for
            // the end user if the given value had no effect on the execution.
            if ($nestedOperations['disconnect'] ?? false) {
                $relation->dissociate();
            }

            if ($nestedOperations['delete'] ?? false) {
                $relation->delete();
            }
        });

        if ($parentRelation && ! $parentRelation instanceof BelongsToMany) {
            // If we are already resolving a nested create, we might
            // already have an instance of the parent relation available.
            // In that case, use it to set the current model as a child.
            $parentRelation->save($model);

            return $model;
        }

        $model->save();

        if ($parentRelation instanceof BelongsToMany) {
            $parentRelation->syncWithoutDetaching($model);
        }

        return $model;
    }
protected static function saveModelWithPotentialParent ( Model $ model , Collection $ args , ? Relation $ parentRelation = null ) : Model { [ $ belongsTo , $ remaining ] = self :: partitionArgsByRelationType ( new ReflectionClass ( $ model ) , $ args , BelongsTo :: class ) ; // Use all the remaining attributes and fill the model $ model -> fill ( $ remaining -> all ( ) ) ; $ belongsTo -> each ( function ( array $ nestedOperations , string $ relationName ) use ( $ model ) : void { /** @var \Illuminate\Database\Eloquent\Relations\BelongsTo $relation */ $ relation = $ model -> { $ relationName } ( ) ; if ( $ create = $ nestedOperations [ 'create' ] ?? false ) { $ belongsToModel = self :: executeCreate ( $ relation -> getModel ( ) -> newInstance ( ) , new Collection ( $ create ) ) ; $ relation -> associate ( $ belongsToModel ) ; } if ( $ connect = $ nestedOperations [ 'connect' ] ?? false ) { // Inverse can be hasOne or hasMany /** @var \Illuminate\Database\Eloquent\Relations\BelongsTo $belongsTo */ $ belongsTo = $ model -> { $ relationName } ( ) ; $ belongsTo -> associate ( $ connect ) ; } if ( $ update = $ nestedOperations [ 'update' ] ?? false ) { $ belongsToModel = self :: executeUpdate ( $ relation -> getModel ( ) -> newInstance ( ) , new Collection ( $ update ) ) ; $ relation -> associate ( $ belongsToModel ) ; } // We proceed with disconnecting/deleting only if the given $values is truthy. // There is no other information to be passed when issuing those operations, // but GraphQL forces us to pass some value. It would be unintuitive for // the end user if the given value had no effect on the execution. if ( $ nestedOperations [ 'disconnect' ] ?? false ) { $ relation -> dissociate ( ) ; } if ( $ nestedOperations [ 'delete' ] ?? false ) { $ relation -> delete ( ) ; } } ) ; if ( $ parentRelation && ! $ parentRelation instanceof BelongsToMany ) { // If we are already resolving a nested create, we might // already have an instance of the parent relation available. // In that case, use it to set the current model as a child. $ parentRelation -> save ( $ model ) ; return $ model ; } $ model -> save ( ) ; if ( $ parentRelation instanceof BelongsToMany ) { $ parentRelation -> syncWithoutDetaching ( $ model ) ; } return $ model ; }
Save a model that maybe has a parent.

@param  \Illuminate\Database\Eloquent\Model  $model
@param  \Illuminate\Support\Collection  $args
@param  \Illuminate\Database\Eloquent\Relations\Relation|null  $parentRelation
@return \Illuminate\Database\Eloquent\Model
Save a model that maybe has a parent .