laravel 创建好数据表之后添加字段


先生成一个新的迁移文件

php artisan make:migration add_users_table

新的迁移文件中写入要添加的字段

// 运行迁移时会被调用
public function up()
{
    Schema::table('users',function (Blueprint $table) {
        $table->string('avatar')->after('name')->default('');
    });
}

// 回滚迁移时会被调用  如果不回滚这里可以不用改 用默认的就可以
public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('avatar');
    });
}

我们将 avatar 字段放在 name 字段后面。而修改表和创建表的区别就是 create 方法改成 table 方法

最后执行

php artisan migrate

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM