我不知道如何使用Laravel框架将新列添加到现有的数据库表中.
我试图用...
<?php
public function up()
{
Schema::create('users', function ($table) {
$table->integer("paid");
});
}
在终端,我执行php artisan migrate:install
和migrate
.
如何添加新列?
我不知道如何使用Laravel框架将新列添加到现有的数据库表中.
我试图用...
<?php
public function up()
{
Schema::create('users', function ($table) {
$table->integer("paid");
});
}
在终端,我执行php artisan migrate:install
和migrate
.
如何添加新列?
要创建迁移,可以在Artisan CLI上使用migrate:make命令.使用特定名称以避免与现有模型发生冲突
对于Laravel 5+:
php artisan make:migration add_paid_to_users_table --table=users
对于Laravel 3:
php artisan migrate:make add_paid_to_users
然后需要使用Schema::table()
方法(因为您访问的是现有表,而不是创建新表).您可以添加如下所示的列:
public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}
别忘了添加回滚选项:
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('paid');
});
}
然后可以运行迁移:
php artisan migrate
这在Laravel 4/Laravel 5的文档中都有很好的介绍:
And 对于Laravel 3:
编辑:
使用$table->integer('paid')->after('whichever_column');
将此字段添加到特定列之后.