Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add migration to set integer to unsigned

Tags:

laravel

I've got a table, and I want to add ->unsigned() to the id. At the moment, the migration looks like: $table->increments('id');

So I want to do a new migration to set that to unsigned, but there's not much in the documentation to do this. Is this the correct way:

public function up()
{
   Schema::table('authors', function($t) {
        $t->unsigned('id', 'authors_id_unsigned');
    });
}


public function down()
{
    Schema::table('authors', function($t) {
            $t->dropUnsigned('authors_id_unsigned');
    });
}

I'm just guessing here because I can't find it in the docs.

like image 985
babbaggeii Avatar asked Feb 13 '14 12:02

babbaggeii


2 Answers

You can modify columns in Laravel 5+, but you have to add the doctrine/dbal dependency to your composer.json file.

public function up()
{
    Schema::table('my_table', function (Blueprint $table) {
        $table->integer('my_column')->unsigned()->change();
    });
}

public function down()
{
    Schema::table('my_table', function (Blueprint $table) {
        $table->integer('my_column')->change();
    });
}
like image 134
Dane Avatar answered Oct 09 '22 17:10

Dane


You can't change that kind of details with the Schema Builder.

To change it, you have to run a raw query. To achieve this, your migration should look something like this:

public function up()
{
   Schema::table('authors', function($t) {
        DB::statement("ALTER TABLE `authors` CHANGE COLUMN `id` `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT FIRST");
    });
}


public function down()
{
    Schema::table('authors', function($t) {
        DB::statement("ALTER TABLE `authors` CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT FIRST");
    });
}
like image 40
Repox Avatar answered Oct 09 '22 16:10

Repox