Skip to content

Instantly share code, notes, and snippets.

@juniormartinxo
Last active May 14, 2022 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juniormartinxo/3239680cb0da5d087f893f0b7ab5d33c to your computer and use it in GitHub Desktop.
Save juniormartinxo/3239680cb0da5d087f893f0b7ab5d33c to your computer and use it in GitHub Desktop.

Laravel Migrations - Comandos comuns

1) Adicionar uma nova coluna em uma tabela do banco de dados

No exemplo abaixo iremos criar uma nova fk de nome job_id à tabela users

php artisan make:migration AddJobIdToUser --table=users

Vá até a pasta database/migrations e você verificará que um novo arquivo foi criado, abra-o e edite os métodos up e down, deixando-os assim:

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->unsignedBigInteger('job_id')->after('id');
    });
}

->after() permite que você informe em posição a coluna estará, no caso acima ela ficará logo abaixo da coluna id

Deixe o método down, da seguinte forma:

public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('job_id');
    });
}

Agora rode a migrate, executando o comando:

php artisan migrate --path=/database/migrations/nome_da_migrations_add_role_to_user.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment