생성하기
1php artisan migrate2 3php artisan make:migration 제목 --create=테이블명4 5php artisan make:migration create_project_table --create=projects
상태확인
1php artisan migrate:status
롤백하기
1// 바로 전단계 2php artisan migrate:rollback --step 3 4// 2번째 단계 5php artisan migrate:rollback --step=2 6 7바로 전단계 롤백은 --step=1 옵션을 사용한다. 8--step=2 를 2개를 롤백한다. 9 10$ php artisan migrate:reset11 12$ php artisan migrate:refresh13$ php artisan migrate:refresh --seed14$ php artisan migrate:refresh --step=515 16# 모든 테이블 삭제하기17$ php artisan migrate:fresh
테이블, 컬럼 존재확인
1if (Schema::hasTable('users') {2 //3}4 5if (Schema:hasColumn('users', 'email') {6 //7}
Migration
1public function up() 2{ 3 Schema::create('notices', function (Blueprint $table) { 4 5 $table->id(); 6 $table->string('subject')->nullable(); 7 $table->text('content')->nullable(); 8 $table->string('name')->nullable(); 9 $table->string('email')->nullable(); 10 $table->integer('visited')->default(0); 11 $table->integer('votes')->default(0); 12 $table->foreignId('user_id'); 13 $table->timestamps(); 14 15 // $table->foreignId('continent_id')->references('id')->on('continents'); 16 }); 17} 18 19 20Schema::create('users', function (Blueprint $table) { 21 $table->id(); 22 $table->string('name'); 23 $table->string('email')->unique(); 24 $table->timestamp('email_verified_at')->nullable(); 25 $table->string('password'); 26 $table->rememberToken(); 27 $table->timestamps(); 28}); 29 30 31Schema::create('role_users', function (Blueprint $table) { 32 $table->unsignedBigInteger('user_id'); 33 $table->unsignedInteger('role_id'); 34 $table->primary(['user_id', 'role_id']); 35 $table->foreign('user_id') 36 ->references('id') 37 ->on('users') 38 ->onUpdate('cascade') 39 ->onDelete('cascade'); 40 $table->foreign('role_id') 41 ->references('id') 42 ->on('roles') 43 ->onUpdate('cascade') 44 ->onDelete('cascade'); 45}); 46 47Schema::create('attachments', function (Blueprint $table) { 48 $table->increments('id'); 49 $table->text('name'); 50 $table->text('original_name'); 51 $table->string('mime'); 52 $table->string('extension')->nullable(); 53 $table->bigInteger('size')->default(0); 54 $table->integer('sort')->default(0); 55 $table->text('path'); 56 $table->text('description')->nullable(); 57 $table->text('alt')->nullable(); 58 $table->text('hash')->nullable(); 59 $table->string('disk')->default('public'); 60 $table->unsignedBigInteger('user_id')->nullable(); 61 $table->string('group')->nullable(); 62 $table->timestamps(); 63}); 64 65Schema::create('attachmentable', function (Blueprint $table) { 66 $table->increments('id'); 67 $table->string('attachmentable_type'); 68 $table->unsignedInteger('attachmentable_id'); 69 $table->unsignedInteger('attachment_id'); 70 71 $table->index(['attachmentable_type', 'attachmentable_id']); 72 73 $table->foreign('attachment_id') 74 ->references('id') 75 ->on('attachments') 76 ->onUpdate('cascade') 77 ->onDelete('cascade'); 78}); 79 80 81if (! Schema::hasTable('notifications')) { 82 Schema::create('notifications', function (Blueprint $table) { 83 $table->uuid('id')->primary(); 84 $table->string('type'); 85 $table->morphs('notifiable'); 86 $table->text('data'); 87 $table->timestamp('read_at')->nullable(); 88 $table->timestamps(); 89 }); 90} 91 92Schema::create('chirps', function (Blueprint $table) { 93 $table->id(); 94 $table->foreignId('user_id')->constrained()->cascadeOnDelete(); 95 $table->string('message'); 96 $table->timestamps(); 97}); 98 99Schema::create('stocks', function (Blueprint $table) {100 $table->id();101 $table->string('code')->unique();102 $table->string('name')->nullable();103 $table->string('mkname')->nullable();104 $table->text('desc')->nullable();105 $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));106 $table->timestamp('updated_at')->nullable();107 //$table->timestamps();108});
Migration 상태보기
1$ php artisan migrate:status
롤백
1php artisan migrate:rollback 2php artisan migrate:rollback --step=5 3 4php artisan migrate:reset 5 6php artisan migrate:refresh 7php artisan migrate:refresh --seed 8php artisan migrate:refresh --step=5 9 10// 모든 테이블 삭제하기11php artisan migrate:fresh
Migration 생성하기
1# 생성 옵션 살펴보기2php artisan make:migration -h3 4php artisan make:migration 파일명 --create=테이블명5 6# 예제7php artisan make:migration create_project_table --create=notices
테이블, 컬럼 존재확인
1if (Schema::hasTable('users') {2 //3}4 5if (Schema:hasColumn('users', 'email') {6 //7}
컬럼 생성
1$table->id(); 2 3$table->binary('data'); 데이터베이스의 BLOB. 4$table->boolean('confirmed'); 데이터베이스의 BOOLEAN. 5$table->enum('choices', ['foo', 'bar']); 데이터베이스의 ENUM. 6 7$table->tinyInteger('numbers'); 데이터베이스의 TINYINT. 8$table->smallInteger('votes'); 데이터베이스의 SMALLINT. 9$table->integer('votes'); 데이터베이스의 INTEGER.10$table->mediumInteger('numbers'); 데이터베이스의 MEDIUMINT.11$table->bigInteger('votes'); 데이터베이스의 BIGINT.12$table->decimal('amount', 5, 2); 유효값과 소수 자릿수를 지정한 DECIMAL13$table->double('column', 15, 8); 15자리, 소수점 8자릿수를 지정한 DOUBLE .14$table->float('amount'); 데이터베이스의 FLOAT.15 16$table->increments('id'); "UNSIGNED INTEGER"에 해당하는 Incrementing ID (프라이머리 키).17$table->bigIncrements('id'); "UNSIGNED BIG INTEGER"에 해당하는 Incrementing ID (프라이머리 키).18 19$table->char('name', 4); CHAR에 해당하며 길이(length)를 가짐.20$table->string('name', 100); VARCHAR에 해당하며 길이(length)를 가짐.21$table->string('email'); VARCHAR에 해당하는 컬럼.22$table->text('description'); 데이터베이스의 TEXT.23$table->mediumText('description'); 데이터베이스의 MEDIUMTEXT.24$table->longText('description'); 데이터베이스의 LONGTEXT.25 26$table->date('created_at'); 데이터베이스의 DATE.27$table->dateTime('created_at'); 데이터베이스의 DATETIME.28$table->dateTimeTz('created_at'); 데이터베이스의 DATETIME (타임존과 함께)29 30$table->ipAddress('visitor'); IP 주소.31 32// 지원하지 않음.33// $table->json('options'); 데이터베이스의 JSON.34// $table->jsonb('options'); 데이터베이스의 JSONB.35 36$table->macAddress('device'); MAC 어드레스.37$table->morphs('taggable'); taggable_id INTEGER와 taggable_type STRING 추가.38$table->rememberToken(); remember_token을 VARCHAR(100) NULL로 추가.39$table->softDeletes(); soft delete할 때 deleted_at 컬럼을 추가함.40 41$table->time('sunrise'); 데이터베이스의 TIME.42$table->timeTz('sunrise'); 데이터베이스의 TIME(타임존과 함께).43$table->timestamp('added_on'); 데이터베이스의 TIMESTAMP.44$table->timestampTz('added_on'); 데이터베이스의 TIMESTAMP (타임존과 함께).45$table->timestamps(); created_at과 updated_at 컬럼을 추가함.46$table->nullableTimestamps(); timestamps()와 동일하지만 NULL 허용.47 48$table->uuid('id'); 데이터베이스의 UUID에 해당.
인덱스 만들기
1$table->primary('id'); 프라이머리 키 추가.2$table->primary(['first', 'last']); 복합 키 추가.3 4$table->unique('email'); 유니크 인덱스 추가.5$table->unique('state', 'my_index_name'); 인덱스의 이름을 지정하기6 7$table->index('state'); 기본적인 인덱스 추가.
Migration 상태보기
1 2->default($value) 컬럼의 "기본"값을 설정합니다 3->autoIncrement() INTEGER 컬럼을 자동으로 증가하는 (auto-increment) (primary key)로 지정합니다 4->nullable($value = true) 컬럼에 NULL 값이 입력되는 것을 허용합니다(기본값) 5->from($integer) 자동 증가 필드의 시작 값을 설정합니다 (MySQL / PostgreSQL) 6 7 8 9->after('column') 컬럼을 다른 컬럼 "뒤"로 옮깁니다 (MySQL)10 11->charset('utf8mb4') 컬럼의 캐릭터셋을 지정합니다 (MySQL)12->collation('utf8mb4_unicode_ci') 컬럼의 collation 지정합니다 (MySQL/PostgreSQL/SQL Server)13->comment('my comment') 컬럼에 코멘트 추가합니다 (MySQL/PostgreSQL)14->first() 컬럼을 테이블의 "맨 처음" 위치로 옮깁니다 (MySQL)15->invisible() 컬럼을 SELECT * 쿼리에 "invisible(보이지 않게)" 만듭니다. (MySQL)16->storedAs($expression) stored generated 컬럼 생성하기 (MySQL)17->unsigned() INTEGER 컬럼을 UNSIGNED 으로 지정 (MySQL)18->useCurrent() CURRENT_TIMESTAMP를 기본값으로 사용하도록 TIMESTAMP 컬럼을 설정합니다.19->useCurrentOnUpdate() 레코드가 수정될 때 CURRENT_TIMESTAMP를 사용하도록 TIMESTAMP 컬럼을 설정합니다.20->virtualAs($expression) virtual generated 컬럼 생성하기 (MySQL)21->generatedAs($expression) 지정한 시퀀스 옵션을 사용하여 ID 컬럼 만들기 (PostgreSQL)22->always() id 컬럼에 입력할 순차 값의 우선 순위를 정의합니다 (PostgreSQL)23->isGeometry() 공간 컬럼 유형을 geometry으로 설정 - 기본 유형은 geography입니다. (PostgreSQL).