Last active
December 30, 2015 19:19
-
-
Save MauMaGau/7873463 to your computer and use it in GitHub Desktop.
Laravel Illuminate Schema : add method to updateColumn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
## Migration ## | |
Schema::table('users', function(Blueprint $table) | |
{ | |
$table->updateColumn('username', 'string')->length(250)->nullable(); | |
}); | |
## \Illuminate\Support\Database\Schema\Blueprint.php ## | |
/** | |
* Indicate that the given columns should be altered. | |
* | |
* @param string $column_name | |
* @param string $type | |
* @return \Illuminate\Support\Fluent | |
*/ | |
public function updateColumn($column_name, $type) | |
{ | |
return $this->addCommand('updateColumn', compact('column_name', 'type')); | |
} | |
## \Illuminate\Database\Schema\Grammars\MySqlGrammar.php ## | |
/** | |
* Compile an update column command. | |
* | |
* @param \Illuminate\Database\Schema\Blueprint $blueprint | |
* @param \Illuminate\Support\Fluent $command | |
* @return string | |
*/ | |
public function compileUpdateColumn(Blueprint $blueprint, Fluent $command) | |
{ | |
$table = $this->wrapTable($blueprint); | |
$type = $this->{'type'.ucfirst($command->type)}($command); | |
return "alter table {$table} modify {$command->columnName} {$type}"; | |
} |
Another thought:
public function compileUpdateColumn(Blueprint $blueprint, Fluent $command)
{
$table = $this->wrapTable($blueprint);
$type = $this->{'type'.ucfirst($command->type)}($command);
$sql = "alter table {$table} modify {$command->columnName} {$type}";
return $this->addModifiers($sql, $blueprint, $command);
}
Otherwise no nullable()
will not create a not null
at the end of the line, an important distinction! And of course we'd miss any other modifiers like after() default() etc etc
Thanks, the addModifiers method is a massive improvement. Included and sent as a pull request.
I am facing issue it, code is not working for me. migration is successfully run but not changing the datatype.
return $this->addModifiers($sql, $blueprint, $command); // where is addmodifier function??
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe lines 17 and 19 should be
$columnName
and'column_name'
respectivelyOr else line 36 should be
$command->column_name
.This is non-working code I know, but for the sake of accuracy :P