Tôi đã một blog mà articles
bảng Schema
được định nghĩa như thế này:Loại bỏ cột từ cơ sở dữ liệu trong Laravel 5.2
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->string('thumb')->nullable();
$table->text('excerpt');
$table->text('body');
$table->string('slug')->unique();
$table->integer('comment_count')->unsigned()->default(0);
$table->integer('view_count')->unsigned()->default(0);
$table->timestamps();
$table->softDeletes();
}
public function down()
{
Schema::drop('articles');
}
Tôi muốn thả các cột comment_count
và view_count
mà không làm mất dữ liệu hiện có trong bảng
tôi xác định một sự chuyển đổi mới như thế này:
class RemoveCommentViewCount extends Migration
{
public function up()
{
//nothing here
}
public function down()
{
Schema::table('articles', function($table) {
$table->dropColumn('comment_count');
$table->dropColumn('view_count');
});
}
}
và tôi đã làm php artisan migrate
. Nó đã di chuyển thành công, nhưng hai cột không bị loại bỏ.
Tôi đang làm gì sai? Làm thế nào tôi có thể thả các cột đó mà không làm mất dữ liệu hiện có trong bảng?
như được chỉ ra bởi @ Sangar82, tôi là phương thức 'up()' được kích hoạt khi bạn chạy 'php artisan migrate'. Phương thức 'down()' được kích hoạt trong trường hợp bạn làm một 'migrate: refresh' hoặc' rollback' như được giải thích dưới đây. – daneczech