protected function replaceRelations($relations, &$stub)
    {
        $textRelations = [];

        foreach ($relations as $key => $relation) {
            $relationStub = $this->stubs['relation'];

            // generate options array
            $options = [];

            if ($relation['type'] != 'morphTo') {
                $options[] = "'" . get_mapped_model($relation['relatedEntity'], false)."'";
            }

            foreach ($relation['options'] as $name => $option) {
                if ($option === null) {
                    $options[] = 'null';
                } elseif ($option === true) {
                    $options[] = 'true';
                } elseif ($option === false) {
                    $options[] = 'false';
                } else {
                    if ($name == 'throughEntity') {
                        $options[] = "'".get_mapped_model($option, false)."'";
                    } elseif ($name != 'morphableClasses') {
                        $options[] = "'".$option."'";
                    }
                }
            }
            
            $options = implode(", ", $options);

            $relationStub = str_replace('{{name}}', $relation['name'], $relationStub);
            $relationStub = str_replace('{{options}}', $options, $relationStub);
            $relationStub = str_replace('{{ucfirst_type}}', ucfirst($relation['type']), $relationStub);
            $relationStub = str_replace('{{type}}', $relation['type'], $relationStub);

            $textRelations[] = $relationStub;

            if ($relation['type'] == 'morphTo'
                || ($relation['type'] == 'morphToMany' && ! $relation['options']['inverse'])) {
                $morphStub = $this->stubs['morph_extension'];

                $morphableClasses = [];
                foreach ($relation['options']['morphableClasses'] as $key => $name) {
                    $morphableClasses[$key] = get_mapped_model($name, false);
                }

                $morphStub = str_replace('{{name}}', $relation['name'], $morphStub);
                $morphStub = str_replace('{{morphName}}', ucfirst($relation['options']['morphName']), $morphStub);
                $morphStub = str_replace('{{types}}', $this->getArrayAsText($morphableClasses, 2), $morphStub);

                $textRelations[] = $morphStub;
            }
        }

        $stub = str_replace('{{relations}}', implode(PHP_EOL . PHP_EOL, $textRelations), $stub);
    }