/** * The up portion of the migration * * @param bool $tableCreated * @return string */ protected function migrationUp($tableCreated = false) { $type = $tableCreated ? "table" : "create"; $content = "\t\tSchema::{$type}('" . $this->model->getTableName() . "', function(Blueprint \$table) {\n"; if (!$tableCreated) { $content .= "\t\t\t" . $this->setColumn('increments', 'id') . ";\n"; } $content .= $this->addColumns(); $content .= $this->addTimestamps(); $content .= $this->addSoftDeletes(); $content .= $this->addForeignKeys(); $content .= $this->dropColumns(); $content .= $this->dropRelationships(); $content .= "\t\t});\n"; if ($this->columnsChanged) { $content .= $this->dropPivotTables(); } else { $content = $this->dropPivotTables(); } foreach ($this->model->getRelationships() as $relation) { if ($relation->getType() == "belongsToMany") { $tableOne = $this->model->tableNameLower(); $tableTwo = $relation->model->tableNameLower(); $tableName = $this->getPivotTableName($tableOne, $tableTwo); if (!$this->isTableCreated($tableName)) { $this->columnsChanged = true; array_push($this->tablesAdded, $tableName); $content .= "\t\tSchema::create('" . $tableName . "', function(Blueprint \$table) {\n"; $content .= "\t\t\t\$table->integer('" . $tableOne . "_id')->unsigned();\n"; $content .= "\t\t\t\$table->integer('" . $tableTwo . "_id')->unsigned();\n"; $content .= "\t\t});\n"; } } else { if ($relation->getType() == "hasOne" || $relation->getType() == "hasMany") { if ($this->tableHasColumn($this->model->lower() . "_id", $relation->model->getTableName())) { if (!$tableCreated) { $content .= "\t\tSchema::table('" . $relation->model->getTableName() . "', function(Blueprint \$table) {\n"; $content .= "\t\t\t\$table->foreign('" . $this->model->tableNameLower() . "_id')->references('id')->on('" . $this->model->getTableName() . "');\n"; $content .= "\t\t});\n"; } } else { if ($this->isTableCreated($relation->model->getTableName()) && !$this->tableHasColumn($this->model->tableNameLower() . "_id", $relation->model->getTableName())) { $this->columnsChanged = true; $column = $this->model->tableNameLower() . "_id"; array_push($this->columnsAdded, $column); $content .= "\t\tSchema::table('" . $relation->model->getTableName() . "', function(Blueprint \$table) {\n"; $content .= "\t\t\t\$table->integer('" . $column . "')->unsigned();\n"; $content .= "\t\t\t\$table->foreign('" . $column . "')->references('id')->on('" . $this->model->getTableName() . "');\n"; $content .= "\t\t});\n"; } } } } } return $content; }