
Schema::__construct 메서드는 데이터베이스 스키마를 생성하고 수정하는 데 사용됩니다. 이 메서드는 데이터베이스 스키마를 조회하는 데 사용되지 않습니다.
이 메서드는 Migration 클래스의 up() 메서드에서 호출됩니다. up() 메서드는 데이터베이스 스키마를 생성하거나 수정하는 코드를 포함합니다.
예를 들어, CreateUsersTable Migration 클래스의 up() 메서드에서 Schema::create() 메서드를 호출하여 'users' 테이블을 생성합니다.
Schema::create() 메서드는 Blueprint 객체를 인자로 받습니다. Blueprint 객체는 테이블의 구조를 정의하는 데 사용됩니다.
다음은 예시 코드입니다.
#hostingforum.kr
php
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
이 코드는 'users' 테이블을 생성합니다. 테이블의 id, name, email 열을 정의하고, email 열은 고유한 값을 갖도록 설정합니다. 또한 created_at 및 updated_at 열을 추가합니다.
Schema::dropIfExists() 메서드는 테이블을 삭제합니다. 이 메서드는 Migration 클래스의 down() 메서드에서 호출됩니다.
2025-07-31 19:32