Skip to content

Instantly share code, notes, and snippets.

@MauMaGau
Last active December 30, 2015 19:19
Show Gist options
  • Save MauMaGau/7873463 to your computer and use it in GitHub Desktop.
Save MauMaGau/7873463 to your computer and use it in GitHub Desktop.
Laravel Illuminate Schema : add method to updateColumn
<?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}";
}
@JasonTolliver
Copy link

I believe lines 17 and 19 should be $columnName and 'column_name' respectively

Or else line 36 should be $command->column_name.

This is non-working code I know, but for the sake of accuracy :P

@JasonTolliver
Copy link

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

@MauMaGau
Copy link
Author

Thanks, the addModifiers method is a massive improvement. Included and sent as a pull request.

@sanjulika
Copy link

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