資料2-PHP Artisan Migrationデータベース表構造

6839 ワード

Artisan Migrationデータベーステーブル構造
  • Migration共通コマンド
  • php artisan migrate                                           #     
    php artisan migrate --force                                   #     ,         
    php artisan migrate:rollback                                  #     /  migrate  
    php atrisan make:migrate                                      #               
    php artisan make:migration create_news_table --create=news    #    news migration   
                                                                     #    /database/migrations/2014_10_12_000000_create_users_table.php
    php artisan make:migration add_auther_column_to_news          #       
    
  • Migrationフィールドタイプ
  • # b
    $table->bigIncrements('id');              #    ID(  ),   「UNSIGNED BIG INTEGER」  。
    $table->bigInteger('votes');              #     BIGINT   。
    $table->binary('data');                   #     BLOB   。
    $table->boolean('confirmed');             #     BOOLEAN   。
    # c
    $table->char('name', 4);                  #     CHAR   ,     。
    # d
    $table->date('created_at');               #     DATE   。
    $table->dateTime('created_at');           #     DATETIME   。
    $table->decimal('amount', 5, 2);          #     DECIMAL   ,        。
    $table->double('column', 15, 8);          #     DOUBLE   ,    15   ,        8   。
    # e
    $table->enum('choices', ['foo', 'bar']);  #     ENUM   。
    # f
    $table->float('amount');                  #     FLOAT   。
    # i
    $table->increments('id');                 #     ID (  ),     「UNSIGNED INTEGER」   。
    $table->integer('votes');                 #     INTEGER   ,  11。
    # j
    $table->json('options');                  #     JSON   。
    $table->jsonb('options');                 #     JSONB   。
    # l
    $table->longText('description');          #     LONGTEXT   。
    # m
    $table->mediumInteger('numbers');         #     MEDIUMINT   。
    $table->mediumText('description');        #     MEDIUMTEXT   。
    $table->morphs('taggable');               #      taggable_id      taggable_type
    # n
    $table->nullableTimestamps();             #   timestamps()   ,     NULL。
    # r
    $table->rememberToken();                  #    remember_token     VARCHAR(100) NULL。
    # s
    $table->smallInteger('votes');            #     SMALLINT   。
    $table->softDeletes();                    #    deleted_at          。
    $table->string('email');                  #     VARCHAR   。
    $table->string('name', 100);              #     VARCHAR   ,     。
    # t
    $table->text('description');              #     TEXT   。
    $table->time('sunrise');                  #     TIME   。
    $table->tinyInteger('numbers');           #     TINYINT   ,  4。
    $table->timestamp('added_on');            #     TIMESTAMP   。
    $table->timestamps();                     #    created_at   updated_at   。
    # u
    $table->uuid('id');                       #     UUID   。
    
  • Migrationフィールドタイプ修飾子
  • ->first()          #            「   」(   MySQL)
    ->after('column')  #            「  」(   MySQL)
    ->nullable()       #         NULL  
    ->default($value)  #       「  」 
    ->unsigned()       #    integer     UNSIGNED,    
    
  • Migrationフィールドタイプ使用可能なインデックス
  • $table->primary('id');              #     。
    $table->primary(['first', 'last']);   #      。
    $table->unique('email');              #       。
    $table->index('state');            #       。
    
  • 常用
  • // increments = unsignedInteger         + autoIncrement   
    $table->increments('id');
    //   int  ,    ,      
    $table->integer('userId')->unsigned()->index()->comment('  ID');
    //       
    $table->foreign('userId')->references('id')->on('users')->onDelete('cascade'); 
    

    フィールドの追加
    // phone      ,column     ,users   
    php artisan make:migration add_phone_column_to_users --table=users
    

    新規フィールドの書き込み
        public function up() {
            Schema::create('users', function (Blueprint $table) {
                $table->string('phone')->comment('     ');
            });
        }
    

    ケース
  • usersテーブル
  • Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('mobile',32)->comment('   ');
        $table->string('cashNum', 60)->default('')->comment('    ');
        $table->string('password')->comment('  ');
        $table->string('tpwd')->comment('    ');
        $table->string('userIcon')->default('')->nullable()->comment('    ');
        $table->string('shopName')->default('')->comment('    ');
        $table->string('recommen')->default('')->comment('   ');
        $table->decimal('balance',12,2)->default(0.00)->comment('  ');
        $table->rememberToken();
        $table->timestamps();
    });
    
  • aaa
  •     public function up() {
            Schema::create('posts', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('category_id')->unsigned()->comment('  ');
                $table->string('title')->comment('  ');
                $table->string('slug')->unique()->index()->comment('  ');
                $table->string('summary')->comment('  ');
                $table->text('content')->comment('  ');
                $table->text('origin')->comment('    ');
                $table->integer('comment_count')->unsigned()->comment('    ');
                $table->integer('view_count')->unsigned()->comment('    ');
                $table->integer('favorite_count')->unsigned()->comment('    ');
                $table->boolean('published')->comment('      ');
                $table->timestamps();
                //Post  category_id      , Category     
                $table->foreign('category_id')
                      ->references('id')
                      ->on('categories')
                      ->onUpdate('cascade')
                      ->onDelete('cascade');
            });
        }
    
  • aaa