[英]Laravel Add a new column to existing table in a migration
我不知道如何使用 Laravel 框架向我现有的数据库表中添加新列。
我尝试使用...编辑迁移文件
<?php
public function up()
{
Schema::create('users', function ($table) {
$table->integer("paid");
});
}
在终端中,我执行php artisan migrate:install
和migrate
。
如何添加新列?
要创建迁移,您可以在 Artisan CLI 上使用 migrate:make 命令。 使用特定名称以避免与现有模型发生冲突
对于 Laravel 3:
php artisan migrate:make add_paid_to_users
对于 Laravel 5+:
php artisan make:migration add_paid_to_users_table --table=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 3 的文档中都有很好的介绍:
对于 Laravel 4 / Laravel 5:
编辑:
使用$table->integer('paid')->after('whichever_column');
在特定列之后添加此字段。
我将为使用 Laravel 5.1 及更高版本的未来读者添加 mike3875 的答案。
为了使事情更快,您可以像这样使用标志“--table”:
php artisan make:migration add_paid_to_users --table="users"
这将自动添加up
和down
方法内容:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
同样,您可以在创建新迁移时使用--create["table_name"]
选项,这将为您的迁移添加更多样板。 小点,但在做大量工作时很有帮助!
通过执行以下命令创建一个新的迁移: make:migration
例子 :
php artisan make:migration add_store_id_to_users_table --table=users
在 database/migrations 文件夹中,您有新的迁移文件,例如:
2018_08_08_093431_add_store_id_to_users_table.php (见评论)
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddStoreIdToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
// 1. Create new column
// You probably want to make the new column nullable
$table->integer('store_id')->unsigned()->nullable()->after('password');
// 2. Create foreign key constraints
$table->foreign('store_id')->references('id')->on('stores')->onDelete('SET NULL');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
// 1. Drop foreign key constraints
$table->dropForeign(['store_id']);
// 2. Drop the column
$table->dropColumn('store_id');
});
}
}
之后运行命令:
php artisan migrate
如果您出于任何原因想要撤消上次迁移,请运行以下命令:
php artisan migrate:rollback
您可以在文档中找到有关迁移的更多信息
如果您使用的是 Laravel 5,则命令为;
php artisan make:migration add_paid_to_users
所有用于制作事物的命令(控制器、模型、迁移等)都已移至make:
命令下。
php artisan 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();
});
Laravel 7
使用 cli 命令创建迁移文件:
php artisan make:migration add_paid_to_users_table --table=users
将在迁移文件夹中创建一个文件,在编辑器中打开它。
添加到函数 up():
Schema::table('users', function (Blueprint $table) {
// Create new column
// You probably want to make the new column nullable
$table->integer('paid')->nullable()->after('status');
}
添加到函数 down(),这将在迁移因某些原因失败时运行:
$table->dropColumn('paid');
使用 cli 命令运行迁移:
php artisan migrate
如果您想向表中添加一列以创建外键约束:
在上述过程的第 3 步中,您将使用以下代码:
$table->bigInteger('address_id')->unsigned()->nullable()->after('tel_number');
$table->foreign('address_id')->references('id')->on('addresses')->onDelete('SET NULL');
在上述过程的第 4 步中,您将使用以下代码:
// 1. Drop foreign key constraints
$table->dropForeign(['address_id']);
// 2. Drop the column
$table->dropColumn('address_id');
这些东西在 Laravel 5.1 上有效。
首先,在您的终端上执行此代码
php artisan make:migration add_paid_to_users --table=users
之后转到您的项目目录并展开目录数据库 - 迁移和编辑文件 add_paid_to_users.php,添加此代码
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('paid'); //just add this line
});
}
之后返回您的终端并执行此命令
php artisan migrate
希望这有帮助。
在 Laravel 8
php artisan make:migration add_columnname_to_tablename_table --table=tablename
然后在创建迁移之后
public function up()
{
Schema::table('users', function (Blueprint $table) {
// 1. Create new column
$table->datatype('column_name')->nullable();
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
// 1. Create new column
$table->dropColumn('column_name');
});
}
然后运行
php artisan migrate
如果您遇到错误,请使用创建表之前的日期重命名迁移名称,然后再次运行php artisan migrate
警告这是一个破坏性的行为。 如果您使用它,请确保首先备份您的数据库
您可以简单地修改现有的迁移文件,例如在表中添加一列,然后在终端中输入:
$ php artisan migrate:refresh
首先回滚之前的迁移
php artisan migrate:rollback
之后,您可以修改现有的迁移文件(添加新的、重命名或删除列),然后重新运行您的迁移文件
php artisan migrate
将列添加到您的迁移文件并运行此命令。
php artisan migrate:refresh --path=/database/migrations/your_file_name.php
尽管正如其他人提到的那样,迁移文件是最佳实践,但在紧要关头,您还可以添加一个包含 tinker 的列。
$ php artisan tinker
这是终端的单行示例:
Schema::table('users', function(\Illuminate\Database\Schema\Blueprint $table){ $table->integer('paid'); })
(这里是为了可读性而格式化的)
Schema::table('users', function(\Illuminate\Database\Schema\Blueprint $table){
$table->integer('paid');
});
首先你必须创建一个迁移,你可以在 laravel artisan CLI 上使用 migrate:make 命令。像 laravel 4 这样的旧 laravel 版本你可以在 Laravel 4 上使用这个命令:
php artisan migrate:make add_paid_to_users
而对于 Laravel 5 版本
对于 Laravel 5+:
php artisan make:migration add_paid_to_users_table --table=users
然后你需要使用 Schema::table() 。 你必须添加列:
public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}
你可以进一步检查这个
如果您不想将蓝图(架构)拆分为两个迁移文件,那么您可以做的最好的事情是从数据库中删除该表,然后重命名迁移文件的最后一个编号并执行
php artisan migrate
这有助于您保护其他表的数据。
运行此命令: php artisan migrate:fresh --seed 它将删除表并重新添加它更新添加到数据库的所有列
你可以做的是像,
Schema::create('users', function ($table) { $table->integer("paid"); });
在编写此写入命令php artisan migrate
或php artisan refresh
我个人更喜欢刷新而不是新迁移,因为如果您执行新迁移,它将删除所有数据,刷新不会。
但唯一的例外是,如果您进行刷新并且表中有任何外键,则它不会重新建立关系,因此您会收到类似错误,
无法添加外键约束
在 laravel 8
php artisan make:migration add_paid_to_users_table --table=users
public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}
在 laravel 9
add a new column in existing table
php artisan make:migration add_paid_to_users_table
if you want to create new migration then do the below code
php artisan make:migration create_users_table --create=users
只需在迁移文件中编辑要添加新列的内容并运行 -->
php工匠迁移:刷新
如果解决方案都不起作用,您可能已经重新创建了迁移文件,然后添加了一个新列并尝试运行php artisan migrate
以更新旧表,该表将尝试创建该表,但该表已经存在,因此会出错。 要解决将迁移文件重命名为以前命名的问题(以日期开头),然后添加新列运行php artisan migrate
,它实际上会更新旧的而不是创建,解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.