如何为Laravel已创建的表添加新字段?

2026-04-03 06:111阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

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

如何为Laravel已创建的表添加新字段?

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

2.增加字段,并在down方法中删除该字段,代码如下:

如何为Laravel已创建的表添加新字段?

phppublic function up(){ Schema::table('users', function (Blueprint $table) { $table->string('api_token')->nullable(); });}

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

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

如何为Laravel已创建的表添加新字段?

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

2.增加字段,并在down方法中删除该字段,代码如下:

如何为Laravel已创建的表添加新字段?

phppublic function up(){ Schema::table('users', function (Blueprint $table) { $table->string('api_token')->nullable(); });}

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