Ruby 程序员学习 Laravel 框架笔记 (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

查看表结构,效果如下:

一般来说,因为这个操作会重新删除所有表的数据,然后会重新跑 migration,所以这种操作只会在开发环境上用。

完结。

本作品采用《CC 协议》,转载必须注明作者和本文链接
咱们的学习圣地是: https://www.qiuzhi99.com,这里有不限于 Nodejs、React、TypeScript、GraphQL 相关的精品课程。
本帖由 Summer 于 6年前 加精
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!