• laravel框架的数据迁移 (摘取)


    生成迁移

    使用 Artisan make:migration 来创建迁移:

    php artisan make:migration create_users_table

    新的迁移文件会被放置在 database/migrations 目录中。每个迁移文件的名称都包含了一个时间戳,以便让 Laravel 确认迁移的顺序。

    --table 和 --create 选项可用来指定数据表的名称,或是该迁移被执行时是否将创建的新数据表。这些选项需在预生成迁移文件时填入指定的数据表:

      

    php artisan make:migration create_users_table --create=users
     
    php artisan make:migration add_votes_to_users_table --table=users

    如果你想为生成的迁移指定一个自定义输出路径,则可以在运行 make:migration 命令时添加 --path 选项。给定的路径必须是相对于应用程序的基本路径。

    迁移结构

    迁移类通常会包含两个方法:up 和 downup 方法可为数据库添加新的数据表、字段或索引,而 down方法则是 up 方法的逆操作。

    你可以在这两个方法中使用 Laravel 数据库结构生成器来创建以及修改数据表。若要了解 Schema 生成器中的所有可用方法,可查询文档。以下的迁移实例会创建一张 flights 数据表:

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    <?php
     
    use IlluminateSupportFacadesSchema;
    use IlluminateDatabaseSchemaBlueprint;
    use IlluminateDatabaseMigrationsMigration;
     
    class CreateFlightsTable extends Migration
    {
        /**
         * 运行数据库迁移
         *
         * @return void
         */
        public function up()
        {
            Schema::create('flights', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->string('airline');
                $table->timestamps();
            });
        }
     
        /**
         * 回滚数据库迁移
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('flights');
        }
    }

    运行迁移

    使用 Artisan 命令 migrate 来运行所有未完成的迁移:

    php artisan migrate

    在生产环境强制执行迁移

    一些迁移操作是具有破坏性的,这意味着可能会导致数据丢失。为了防止有人在生产环境中运行这些命令,系统会在这些命令被运行之前与你进行确认。如果要强制忽略系统的提示运行命令,则可以使用 --force 标记:

    php artisan migrate --force
  • 相关阅读:
    DWVA系列-2-csrf Medium等级 绕过refer的csrf 有点小曲折
    DWVA系列-2-csrf low等级
    DWVA系列-1-brute Impossible等级 预防SQL注入 一次性token+预编译
    DWVA系列-1-brute medium等级
    DWVA系列-1-brute low等级
    获取SD卡剩余容器
    MD5Encoder
    本地缓存
    内存缓存
    网络缓存
  • 原文地址:https://www.cnblogs.com/Rawan/p/12024983.html
Copyright © 2020-2023  润新知