Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rename column in laravel using migration?

I have columns as mentioned bellow:

public function up() {     Schema::create('stnk', function(Blueprint $table)     {         $table->increments('id');         $table->string('no_reg', 50)->unique();         $table->string('no_bpkb', 50)->unique();         $table->string('nama_pemilik', 100);         $table->string('alamat');         $table->string('merk', 50);         $table->string('tipe', 50);         $table->string('jenis', 50);         $table->smallInteger('tahun_pembuatan');         $table->smallInteger('tahun_registrasi');         $table->smallInteger('isi_silinder');         $table->string('no_rangka', 50);         $table->string('no_mesin', 50);         $table->string('warna', 50);         $table->string('bahan_bakar', 50);         $table->string('warna_tnkb', 50);         $table->string('kode_lokasi', 50);         $table->date('berlaku_sampai');         $table->timestamps();          $table->index('created_at');         $table->index('updated_at');     });  } 

I have made seeder to stnk table

Now I want to rename id to id_stnk.
I've added a "doctrine / dbal" in the "composer" and do a composer update.

I've made migration php artisan migration:make rename_column.
Then I've added new method to rename_column:

Schema::table('stnk', function(Blueprint $table) {     $table->renameColumn('id', 'id_stnk');  }); 

And then I've tried to run command php artisan migrate but I got error as mentioned bellow:

[Ulluminate\Database\QueryException] SQLSTATE[HY000]: General error: 1025 Error on rename of './my_database/#sql -447_33' to './my_database/stnk' (error: 150) (SQL: ALTER TABLE stnk CHANGE id id_stnk INT UNSIGENED AUTO_INCREMENT NOT NULL)  [PDOException] SQLSTATE[HY000]: General error: 1025  Error on rename  of './my_database/#sql -447_33' to './my_database/stnk' (error: 150) 
like image 442
Ariasa Avatar asked Oct 23 '14 06:10

Ariasa


People also ask

How do I change column name and data type in Laravel migration?

Following worked for me. Definitely you need to install doctrine/dbal to make this work, using following command in terminal. open your migration file and write down below. Schema::table('yourTable', function (Blueprint $table) { $table->string('column_name','4294967295')->change(); });

Can I rename migration in Laravel?

Yes, you can name them however you want, but they will run in alphabetical order (which is why laravel timestamps them). Also you can change a migrations name after the fact (you've already run the migration).

How do I rename a table in Laravel migration?

To change a table name, you can do this: Schema::rename($currentTableName, $newTableName); You can use the drop or dropIfExists methods to remove an existing table: Schema::drop('users'); Schema::dropIfExists('users');


1 Answers

You need to create another migration file - and place it in there:

Run

Laravel 4:    php artisan migrate:make rename_stnk_column Laravel 5:    php artisan make:migration rename_stnk_column 

Then inside the new migration file place:

class RenameStnkColumn extends Migration {      public function up()     {         Schema::table('stnk', function(Blueprint $table) {             $table->renameColumn('id', 'id_stnk');         });     }       public function down()     {         Schema::table('stnk', function(Blueprint $table) {             $table->renameColumn('id_stnk', 'id');         });     }  } 
like image 126
Laurence Avatar answered Oct 09 '22 04:10

Laurence