Laravel Logo

I’ve had trouble knowing what to put in my migrations’ up() and down() functions. The Laravel Docs include a lot of information but I wanted a quick reference guide for myself to convert Laravel migrations to MySQL commands because that’s what I’m used to. :-)

I also spend some time compiling this into a Cheetsheet if you want a printable version of this post.

Create a New Table

php artisan make:migration --create=roles name_of_migration

or

php artisan make:migration create_roles

Modify an Existing Table

php artisan make:migration --table=roles name_of_migration

or

php artisan make:migration add_name_to_roles

Table Level

PHP Resulting SQL
Up:
Schema::create('roles', function (Blueprint $t) {
    $t->bigIncrements('id');
});
Down:
Schema::dropIfExists('roles');
create table `roles` (
    `id` bigint unsigned 
    not null
    auto_increment 
    primary key
) default character set utf8mb4 
collate 'utf8mb4_unicode_ci'
Up:
Schema::rename($from, $to);
Down:
Schema::rename($to, $from);
RENAME TABLE `$from` TO `$to`

Modifiers

Item Resulting SQL
$table->boolean('active')->after('id');
alter table `roles`
    add `active` tinyint(1) not null after `id`
$table->boolean('active')->default('1');
alter table `roles`
    add `active` tinyint(1) not null default '1'
$table->boolean('active')->nullable();
alter table `roles`
    add `active` tinyint(1) null
$table->boolean('active')->nullable(false);
alter table `roles`
    add `active` tinyint(1) not null

Columns

PHP Resulting SQL
Up:
$table->timestamps();
Down:
$table->dropTimestamps();
alter table `roles`
    add `created_at` timestamp null, 
    add `updated_at` timestamp null
Up:
$table->softDeletes();
Down:
$table->dropSoftDeletes();
alter table `roles`
    add `deleted_at` timestamp null
Up:
$table->bigIncrements('columnName');
Down:
$table->dropColumn('columnName');
alter table `roles`
    add `columnName` 
    bigint unsigned 
    not null 
    auto_increment 
    primary key
Up:
$table->string('columnName');
Down:
$table->dropColumn('columnName');
alter table `roles`
    add `columnName` varchar(255) not null
Up:
$table->integer('columnName');
Down:
$table->dropColumn('columnName');
alter table `roles`
    add `columnName` int not null
Up:
$table
    ->timestamp('columnName')
    ->default(DB::raw('CURRENT_TIMESTAMP'));
Down:
$table->dropColumn('columnName');
alter table `roles`
    add `columnName` 
    timestamp 
    not null 
    default CURRENT_TIMESTAMP
Up:
$table->unsignedBigInteger('columnName');
Down:
$table->dropColumn('columnName');
alter table `roles`
    add `columnName` bigint unsigned not null

Indexes and Relationships

PHP Resulting SQL
Up:
$table
    ->foreign('user_id')
    ->references('id')
    ->on('users');
Down:
$table->dropForeign('roles_user_id_foreign');
alter table `roles`
    add constraint 
    `roles_user_id_foreign` 
    foreign key (`user_id`) 
    references `users` (`id`)
Up:
$table
    ->string('name', 50)
    ->change();
Up:
$table->renameColumn('from', 'to');
Up:
$table->index(['user_id', 'role_id']);
Down:
$table->dropIndex(['user_id', 'role_id']);
alter table `userroles` 
    add index `roles_user_id_role_id_index`
    (`user_id`, `role_id`)
Up:
$table->unique(['user_id', 'role_id']);
Down:
$table->dropUnique(['user_id', 'role_id']);
alter table `roles`
    add unique 
    `roles_user_id_role_id_unique`
    (`user_id`, `role_id`)