Laravel 5
Примеры миграций в Laravel 5
Примеры 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name')->nullable(); $table->integer('contact')->unsigned()->default(1); $table->string('email')->unique(); $table->text('description')->nullable(); $table->boolean('is_active')->default(false)->after('contact'); $table->string('password'); $table->enum('type',['admin','teacher','student']); $table->rememberToken(); $table->softDeletes(); $table->timestamp('failed_at')->useCurrent(); $table->timestamps(); }); } |
1 2 3 4 |
public function down() { Schema::drop('users'); } |
Примеры 2
1 2 3 4 5 |
Schema::table('users', function (Blueprint $table) { $table->renameColumn('old_name', 'new_name'); $table->dropColumn('column_name'); $table->unique('alias'); }); |
1 2 3 4 |
Schema::table('users', function (Blueprint $table) { $table->dropTimestamps(); $table->dropUnique('users_alias_unique'); }); |
Примеры 3
1 2 3 4 5 6 7 8 9 10 |
Schema::create('files', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned(); $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); }); Schema::table('files', function($table) { $table->foreign('user_id')->references('id')->on('users') ->onUpdate('cascade')->onDelete('cascade'); }); |
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 34 35 36 37 |
DB::statement('ALTER TABLE users ENGINE = InnoDB'); $table->primary(['user_id', 'file_id']); // смена движка для всех таблиц $manager = Schema::getConnection()->getDoctrineSchemaManager(); foreach ($manager->listTableNames() as $table) { DB::statement(sprintf('ALTER TABLE %s ENGINE = InnoDB', $table)); } // удаление внешнего ключа Schema::table('city', function (Blueprint $table) { $table->dropForeign(['region_id']); $table->dropColumn('region_id'); }); //индекс $table->index(['user_id'],'user_id_idx'); //отключение проверки внешних ключей Schema::disableForeignKeyConstraints(); Schema::table('operations', function (Blueprint $schema) { $schema->integer('created_by')->unsigned()->nullable(); $schema->foreign('created_by')->references('id')->on('users')->onDelete('set null'); }); Schema::enableForeignKeyConstraints(); //изменение столбца (надо установить doctrine/dbal) $table->integer('user_id')->unsigned()->nullable()->change(); //удаление с проверкой if (Schema::hasColumn('users', 'role_id')) { Schema::table('users', function ($table) { $table->dropColumn('role_id'); }); } |