如何使用Laravel为现有表添加新字段?

更新于
2026-09-24 22:22:45
1阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计149个文字,预计阅读时间需要1分钟。

如何使用Laravel为现有表添加新字段?

1. 创建 migration 文件,例如为 user 表增加字段 api_token php artisan make:migration add_api_token_to_users --table=users

2. 增加字段,并在 down 方法中删除该字段 php public function up() { Schema::table('users', function (Blueprint $table) { $table->string('api_token')->nullable(); }); }

如何使用Laravel为现有表添加新字段?

public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('api_token'); }); }

1.創建migration文件,比如這裡是為user表增加字段api_token

php artisan make:migration add_api_token_to_users --table=users 2.增加字段,記得要在down中dropColumn

public function up() { Schema::table('users', function (Blueprint $table) { $table->string('api_token',64)->unique(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn(['api_token']); }); } 3.執行生成

php artisan migration

本文共计149个文字,预计阅读时间需要1分钟。

如何使用Laravel为现有表添加新字段?

1. 创建 migration 文件,例如为 user 表增加字段 api_token php artisan make:migration add_api_token_to_users --table=users

2. 增加字段,并在 down 方法中删除该字段 php public function up() { Schema::table('users', function (Blueprint $table) { $table->string('api_token')->nullable(); }); }

如何使用Laravel为现有表添加新字段?

public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('api_token'); }); }

1.創建migration文件,比如這裡是為user表增加字段api_token

php artisan make:migration add_api_token_to_users --table=users 2.增加字段,記得要在down中dropColumn

public function up() { Schema::table('users', function (Blueprint $table) { $table->string('api_token',64)->unique(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn(['api_token']); }); } 3.執行生成

php artisan migration