我不知道如何使用 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 的文档中都有很好的介绍:
架构生成器
迁移
对于 Laravel 3:
架构生成器
迁移
编辑:
使用 $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"]
选项,这将为您的迁移添加更多样板。小点,但在做大量它们时很有帮助!
Blueprint
。只需澄清一点即可。
laravel 5.6 及以上
如果您想将新列作为 FOREIGN KEY 添加到现有表中。
通过执行以下命令创建新迁移: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
您可以在 docs 中找到有关迁移的更多信息
$table->dropForeign('posts_store_id_foreign'); $table->dropColumn('store_id');
如果您使用的是 Laravel 5,则命令为;
php artisan make:migration add_paid_to_users
所有用于制作事物的命令(控制器、模型、迁移等)都已移至 make:
命令下。
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
您可以在初始 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();
});
文档对此相当详尽,从 version 3 到 version 4 并没有太大变化。
create_users_table
,然后如果我要添加列:add_email_password_columns_to_users
。
add_
”动词来跟踪更改。这样更容易跟踪版本控制等的更改,因为每次迭代都会创建一个新的添加文件。如果您只是继续修改“create_
”,则很难知道 x 员工是否通过删除索引或添加新列等搞砸了一些事情。至少这在我看来是有道理的! :)
拉拉维尔 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
希望这有帮助。
首先回滚之前的迁移
php artisan migrate:rollback
之后,您可以修改现有的迁移文件(添加新的、重命名或删除列),然后重新运行您的迁移文件
php artisan migrate
警告这是破坏性行为。如果你使用这个确保你首先备份你的数据库
您可以简单地修改现有的迁移文件,例如在表中添加一列,然后在终端中键入:
$ php artisan migrate:refresh
将列添加到您的迁移文件并运行此命令。
php artisan migrate:refresh --path=/database/migrations/your_file_name.php
首先你必须创建一个迁移,你可以在 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');
});
}
尽管迁移文件是其他人提到的最佳实践,但在紧要关头,您还可以添加一个带有 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');
});
如果您不想将蓝图(架构)拆分为两个迁移文件,那么您可以做的最好的事情是从数据库中删除表,然后重命名迁移文件的最后一个数字并执行
php artisan migrate
这有助于您保护其他表的数据。
运行以下命令: php artisan migrate:fresh --seed 它将删除表并重新添加它,更新添加到数据库的所有列
你可以做的是,
Schema::create('users', function ($table) { $table->integer("paid"); });
在编写此写入命令 php artisan migrate
或 php artisan refresh
之后,我个人更喜欢刷新而不是全新迁移,因为如果您执行全新迁移,它将删除所有数据,刷新不会。
但唯一的例外是如果你刷新并且表中有任何外键,所以它不会重新建立关系,所以你会得到错误,
无法添加外键约束
如果解决方案都不起作用,您可能已经重新创建了迁移文件,然后添加了一个新列并尝试运行 php artisan migrate
以更新旧表,该表将尝试创建该表,但该表已经存在,因此会出错。要解决将迁移文件重命名为以前命名的问题(以日期开头),然后添加新列运行 php artisan migrate
,它将实际更新旧列而不是创建,解决了我的问题。
不定期副业成功案例分享
php artisan migrate
php artisan make:migration add_paid_to_users