Migration 向Laravel迁移模式添加字段
我读过拉威尔的文档和其他论坛,但对我来说根本不起作用。我已经成功迁移了一个表,现在我想添加一个字段,将模式更改为“table”,但我得到的只是“无需迁移” 这就是我所做的 迁移:Migration 向Laravel迁移模式添加字段,migration,laravel-5,Migration,Laravel 5,我读过拉威尔的文档和其他论坛,但对我来说根本不起作用。我已经成功迁移了一个表,现在我想添加一个字段,将模式更改为“table”,但我得到的只是“无需迁移” 这就是我所做的 迁移: <?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateProductTable extends Migration { /** *
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product', function(Blueprint $table)
{
$table->text('image');
$table->integer('stock');
$table->integer('amount');
$table->string('color');
$table->string('dimension');
$table->integer('ordered');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('product');
}
}
然后再次运行php artisan migrate
,但我只得到了无需迁移的内容
我还删除了不起作用的Blueprint
migrate:refresh
和migrate:reset
执行此操作,但这不是我想要的,因为它也会删除数据。我认为这是您想要执行的操作,请按顺序执行以下步骤
php artisan make:migration create_products_table
充实create_products_table.php中的up和down方法(注意:迁移文件前面会有一个时间戳,这将决定运行php artisan migrate
时迁移的执行顺序)
public function up()
{
Schema::create('product', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->text('description');
$table->integer('stock');
$table->integer('amount');
$table->string('color');
$table->string('dimension');
$table->integer('ordered');
$table->timestamps();
});
}
public function down()
{
Schema::drop('product');
}
然后运行php artisan migrate来创建表
现在您已经创建了products表,但现在您希望更改该表并向其添加一些额外字段。请执行以下操作:
php artisan make:migration alter\u products\u table\u add\u image\u active
现在您有了一个单独的迁移来更改表,您不想编辑现有的表,这不是迁移的工作方式。它们基本上保留了数据库构建的历史记录,因此任何调整都应该放在新迁移中,因此如果您碰巧回滚,您可以恢复到以前的状态,这是通过使用他使用了一种与down方法几乎相反的down方法
现在在alter迁移中充实上下方法,alter\u products\u table\u add\u image\u active.php
(注意:文件前面会自动加上时间戳)
这应该可以让您启动并运行!如果您遇到任何问题,请告诉我。当我们需要修改表时,我们可以在migrate命令中使用--table,例如在menu table migrate中使用sample
php artisan make:migrate [your migrate name] --table=menus
啊哈!很好!但是必须将$table->int('active');
更改为$table->integer('active');
。非常感谢!很高兴它对您有效。我很快会更新我的答案。我没有发现,非常感谢。
public function up()
{
Schema::table('product', function(Blueprint $table)
{
$table->text('image');
$table->int('active');
});
}
public function down()
{
// here you're not dropping the whole table, only removing the newly added columns
Schema::table('club', function(Blueprint $table){
$table->dropColumn('image');
$table->dropColumn('active');
});
}
php artisan make:migrate [your migrate name] --table=menus