mak
mak

Reputation: 3

How to remove only fields I just added with Laravel migration?

I'm adding these values to an existing table. In my down() function, how do I remove only the items I just added? truncate() will remove all the items in the table. I can use DB::table('user_account_cancel_reasons')->where('action', '==', 'cancel')->delete();, but that doesn't guarantee only removing the items in this migration. Using Laravel 7.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddNewFieldsToUserAccountChangeReasons extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        DB::table('user_account_change_reasons')->insert(
            [
                [
                    'reason' => 'Health Issues',
                    'action' => 'cancel',
                    'created_at' => DB::raw('CURRENT_TIMESTAMP'),
                    'updated_at' => DB::raw('CURRENT_TIMESTAMP')
                ],
                [
                    'reason' => 'Did not use',
                    'action' => 'cancel',
                    'created_at' => DB::raw('CURRENT_TIMESTAMP'),
                    'updated_at' => DB::raw('CURRENT_TIMESTAMP')
                ],
                [
                    'reason' => 'Other',
                    'action' => 'cancel',
                    'created_at' => DB::raw('CURRENT_TIMESTAMP'),
                    'updated_at' => DB::raw('CURRENT_TIMESTAMP')
                ],
             
            ]

        );
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('user_account_change_reasons', function(Blueprint $table) {
            DB::table('user_account_change_reasons')->truncate();
        });
    }
}

Upvotes: 0

Views: 777

Answers (1)

Nimesh Katariya
Nimesh Katariya

Reputation: 11

Try, i solved it

public function down()
{
    Schema::table('user_account_change_reasons', function(Blueprint $table) 
        {
        DB::table('user_account_change_reasons')->where('action', 
          'cancel')->delete();
    });
}

Upvotes: 1

Related Questions