2015-10-05 70 views
15

以下是我的我的2015_09_14_051851_create_orders_table.php。我想改變$ table-> integer('category_id');作爲新遷移的字符串。laravel遷移表字段的類型變化

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateOrdersTable extends Migration { 

    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('orders', function(Blueprint $table) 
     { 
      $table->increments('id'); 
      $table->string('num'); 
      $table->integer('user_id'); 

      $table->text('store_name'); 
      $table->integer('store_name_publication'); 

      $table->string('postal_code', 255); 
      $table->string('phone_number', 255); 

      $table->text('title'); 
      $table->text('description'); 

      $table->string('list_image_filename1', 255); 
      $table->string('list_image_filename2', 255)->nullable(); 
      $table->string('list_image_filename3', 255)->nullable(); 
      $table->string('list_image_filename4', 255)->nullable(); 
      $table->string('list_image_filename5', 255)->nullable(); 

      $table->integer('term'); 

      $table->datetime('state0_at')->nullable(); 
      $table->datetime('state1_at')->nullable(); 
      $table->datetime('state2_at')->nullable(); 
      $table->datetime('state3_at')->nullable(); 
      $table->datetime('state4_at')->nullable(); 
      $table->datetime('state5_at')->nullable(); 
      $table->datetime('state6_at')->nullable(); 
      $table->datetime('state7_at')->nullable(); 
      $table->datetime('state8_at')->nullable(); 
      $table->datetime('state9_at')->nullable(); 
      $table->datetime('state10_at')->nullable(); 

      $table->integer('category_id'); 
      $table->integer('target_customer_sex'); 
      $table->integer('target_customer_age'); 

      $table->integer('payment_order'); 
      $table->integer('num_comment'); 
      $table->integer('num_view'); 
      $table->string('num_pop'); 

      $table->integer('money'); 
      $table->integer('point'); 

      $table->datetime('closed_at'); 
      $table->timestamps(); 
      $table->softDeletes(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('orders'); 
    } 

} 
+0

'$表 - >字符串( 'CATEGORY_ID');' – aldrin27

+0

沒有我想用其他遷移文件只更改該類型 2015_10_05_021049 _change_category_id_to_orders_table –

回答

19

要對現有數據庫進行一些更改,可以通過在遷移中使用change()來修改列類型。

這是你能做什麼

Schema::table('orders', function ($table) { 
    $table->string('category_id')->change(); 
}); 

請注意,您需要添加學說/ DBAL依賴於composer.json 更多的信息,您可以在這裏找到它http://laravel.com/docs/5.1/migrations#modifying-columns

+1

現在我正在使用laravel 4.2 –

14

standard solution沒對我而言,將TEXT更改爲LONGTEXT

我不得不這樣說:

public function up() 
{ 
    DB::statement('ALTER TABLE mytable MODIFY mycolumn LONGTEXT;'); 
} 

public function down() 
{ 
    DB::statement('ALTER TABLE mytable MODIFY mycolumn TEXT;'); 
} 

這可能是一個學說的問題。更多信息here

另一種方法是使用字符串()方法,並將其值設置爲文本類型最大長度:

Schema::table('mytable', function ($table) { 
     // Will set the type to LONGTEXT. 
     $table->string('mycolumn', 4294967295)->change(); 
    }); 
+2

這對DECIMAL列特別有用。 – dmmd

+1

謝謝你,你已經救了我的一天) –

+1

我可以證實這種行爲。爲了將一列從文本更改爲中文,我必須這樣做: $ table-> string('messages',16777215) - > nullable() - > change(); – Antonio

0

對我來說,解決方案只是與指數

更換籤名完整的代碼

Schema::create('champions_overview',function (Blueprint $table){ 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 
     $table->integer('cid')->index(); 
     $table->longText('name'); 
    }); 


    Schema::create('champions_stats',function (Blueprint $table){ 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 
     $table->integer('championd_id')->index(); 
     $table->foreign('championd_id', 'ch_id')->references('cid')->on('champions_overview'); 
    });