我不知道如何使用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。
如何添加新列?
当前回答
你可以像这样在初始的Schema::create方法中添加新列:
Schema::create('users', function($table) {
$table->integer("paied");
$table->string("title");
$table->text("description");
$table->timestamps();
});
如果您已经创建了一个表,您可以通过创建一个新的迁移并使用Schema::table方法向该表添加额外的列:
Schema::table('users', function($table) {
$table->string("title");
$table->text("description");
$table->timestamps();
});
关于这一点,文档相当详尽,从版本3到版本4没有太大的变化。
其他回答
你可以像这样在初始的Schema::create方法中添加新列:
Schema::create('users', function($table) {
$table->integer("paied");
$table->string("title");
$table->text("description");
$table->timestamps();
});
如果您已经创建了一个表,您可以通过创建一个新的迁移并使用Schema::table方法向该表添加额外的列:
Schema::table('users', function($table) {
$table->string("title");
$table->text("description");
$table->timestamps();
});
关于这一点,文档相当详尽,从版本3到版本4没有太大的变化。
如果您正在使用Laravel 5,命令将是;
php artisan make:migration add_paid_to_users
所有用于制作东西的命令(控制器、模型、迁移等)都被移动到make:命令下。
PHP工匠迁移仍然是一样的。
首先回滚之前的迁移
php artisan migrate:rollback
之后,您可以修改现有的迁移文件(添加新列、重命名列或删除列),然后重新运行迁移文件
php artisan migrate
你能做的就是,
Schema::create('users', function ($table) {$table->integer("paid");});
写完这个写命令后,php artisan迁移或刷新 我个人更喜欢的是刷新而不是新迁移,因为如果你做新迁移,它会删除所有的数据刷新不会。
但唯一的例外是,如果你进行了刷新如果你在表中有任何外键那么它就不会重新建立关系所以你会得到这样的错误,
不能添加外键约束
这个东西在laravel 5.1上工作。
首先,在终端上执行这段代码
php artisan make:migration add_paid_to_users --table=users
然后转到项目目录,展开目录database - migration,编辑文件add_paid_to_users.php,添加这段代码
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('paid'); //just add this line
});
}
然后返回到您的终端并执行此命令
php artisan migrate
希望这对你有所帮助。