2015-10-05 2 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

'$ table-> строка ('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 Didn Не работайте для меня при изменении типа от ТЕКСТ до 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'); 
    });