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年前 加精
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

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