/** * Update the field type column to the table. * * @param Blueprint $table * @param AssignmentInterface $assignment */ public function updateColumn(Blueprint $table, AssignmentInterface $assignment) { // Skip if no column type. if (!$this->fieldType->getColumnType()) { return; } // Skip if the column doesn't exists. if (!$this->schema->hasColumn($table->getTable(), $this->fieldType->getColumnName())) { return; } /** * Update the column to the table. * * @var Blueprint|Fluent $column */ $column = call_user_func_array([$table, $this->fieldType->getColumnType()], array_filter([$this->fieldType->getColumnName(), $this->fieldType->getColumnLength()])); $column->nullable(!$assignment->isTranslatable() ? !$assignment->isRequired() : true)->change(); if (!str_contains($this->fieldType->getColumnType(), ['text', 'blob'])) { $column->default(array_get($this->fieldType->getConfig(), 'default_value')); } /** * Mark the column unique if desired and not translatable. * Otherwise, drop the unique index. */ $connection = $this->schema->getConnection(); $manager = $connection->getDoctrineSchemaManager(); $doctrine = $manager->listTableDetails($connection->getTablePrefix() . $table->getTable()); // The unique index name. $unique = md5('unique_' . $this->fieldType->getColumnName()); /** * If the assignment is unique and not translatable * and the table does not already have the given the * given table index then go ahead and add it. */ if ($assignment->isUnique() && !$assignment->isTranslatable() && !$doctrine->hasIndex($unique)) { $table->unique($this->fieldType->getColumnName(), $unique); } /** * If the assignment is NOT unique and not translatable * and the table DOES have the given table index * then we need to remove. */ if (!$assignment->isUnique() && !$assignment->isTranslatable() && $doctrine->hasIndex($unique)) { $column->dropIndex(md5('unique_' . $this->fieldType->getColumnName())); } }