/** * Replace [model] tags in template with the model name * * @param $fileContents * @return mixed */ private function replaceModels($fileContents) { $modelReplaces = array('[model]' => $this->model->lower(), '[Model]' => $this->model->upper(), '[models]' => $this->model->plural(), '[Models]' => $this->model->upperPlural()); foreach ($modelReplaces as $model => $name) { $fileContents = str_replace($model, $name, $fileContents); } return $fileContents; }
/** * Get the name of the pivot table of current relation * and specified model * * @param Model $model * @return string */ public function getPivotTableName(Model $model) { $tableOne = $this->model->lower(); $tableTwo = $model->lower(); if (strcmp($tableOne, $tableTwo) > 1) { $tableName = $tableTwo . "_" . $tableOne; } else { $tableName = $tableOne . "_" . $tableTwo; } return $tableName; }
/** * For relationships that have been removed, this is the reverse to add them * * @param Relation[] $removedRelationships * @param string $tableName * @return string */ private function rollbackForRemovedProperties($removedRelationships, $tableName) { $functionContents = ""; foreach ($removedRelationships as $relation) { if ($relation->getType() == "belongsToMany") { $functionContents .= "\t\tSchema::create('" . $relation->getPivotTableName($this->model) . "', function(Blueprint \$table) {\n"; $functionContents .= "\t\t\t\$table->integer('" . $relation->getForeignKeyName() . "')->unsigned();\n"; $functionContents .= "\t\t\t\$table->integer('" . $this->model->lower() . "_id')->unsigned();\n"; $functionContents .= "\t\t\t\$table->foreign('" . $relation->getForeignKeyName() . "')->references('id')->on('" . $relation->getRelatedModelTableName() . "');\n"; $functionContents .= "\t\t\t\$table->foreign('" . $this->model->lower() . "_id')->references('id')->on('" . $this->model->getTableName() . "');\n"; $functionContents .= "\t\t});\n"; } else { $functionContents .= "\t\tSchema::table('" . $tableName . "', function(Blueprint \$table) {\n"; $functionContents .= "\t\t\t\$table->integer('" . $relation->getForeignKeyName() . "')->unsigned();\n"; $functionContents .= "\t\t\t\$table->foreign('" . $relation->getForeignKeyName() . "')->references('id')->on('" . $relation->getRelatedModelTableName() . "');\n"; $functionContents .= "\t\t});\n"; } } return $functionContents; }