Hacer una migración para agregar una columna

  • Autor de la entrada:
  • Categoría de la entrada:Laravel
  • Comentarios de la entrada:0 comentarios
  1. Create a migration file using cli command: php artisan make:migration add_paid_to_users_table --table=users
  2. A file will be created in the migrations folder, open it in an editor.
  3. Add to the function up():
Schema::table('users', function (Blueprint $table) {
    // Create new column
    // You probably want to make the new column nullable
    $table->integer('paid')->nullable()->after('status');
}
  1. Add to the function down(), this will run in case migration fails for some reasons:$table->dropColumn('paid');
  2. Run migration using cli command:php artisan migrate

In case you want to add a column to the table to create a foreign key constraint:

In step 3 of the above process, you’ll use the following code:

$table->bigInteger('address_id')->unsigned()->nullable()->after('tel_number');

$table->foreign('address_id')->references('id')->on('addresses')->onDelete('SET NULL');

In step 4 of the above process, you’ll use the following code:

// 1. Drop foreign key constraints
$table->dropForeign(['address_id']);
// 2. Drop the column
$table->dropColumn('address_id');

Deja un comentario