多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 18. CURD 实践之改变用户表结构 学了这么多 laravel 的知识,该来实践了,至少要会基本的增删改查吧,接下来的几篇都会讲解这方面的知识。 之前我们有一个 `user` 表,现在我们往里面加两个属性,一个是用户名 `username`,另一个是出生日期 `dob`。 找到创建 `user` 表的 migration 文件:`database/migrations/2014_10_12_000000_create_users_table.php`。 把它修改如下: ``` // database/migrations/2014_10_12_000000_create_users_table.php <?php ... class CreateUsersTable extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); ... // 新增下面这两行 $table->string('username', 32); $table->date('dob'); ... $table->timestamps(); }); } public function down() { Schema::dropIfExists('users'); } } ``` 现在我们执行一条命令,把所有表结构重建。 ``` $ php artisan migrate:refresh ``` 执行完输出的内容大约是这样的: ``` Rolling back: 2017_08_04_132046_create_photos_table Rolled back: 2017_08_04_132046_create_photos_table Rolling back: 2014_10_12_100000_create_password_resets_table Rolled back: 2014_10_12_100000_create_password_resets_table Rolling back: 2014_10_12_000000_create_users_table Rolled back: 2014_10_12_000000_create_users_table Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table Migrating: 2017_08_04_132046_create_photos_table Migrated: 2017_08_04_132046_create_photos_table ``` 查看表结构,效果如下: ![](https://box.kancloud.cn/80199d2f5b94c62b3e048f0b97fb38fc_1772x942.jpg) 一般来说,因为这个操作会重新删除所有表的数据,然后会重新跑 migration,所以这种操作只会在开发环境上用。 完结。