<?php

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

class AddForeignKeyToAffiliatesCreatedBy extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('affiliates', function (Blueprint $table) {
            // Add foreign key constraint to created_by column
            $table->foreign('created_by')
                  ->references('id')
                  ->on('users')
                  ->onDelete('set null'); // Set to null if user is deleted, preserving affiliate record
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('affiliates', function (Blueprint $table) {
            // Drop the foreign key constraint
            $table->dropForeign(['created_by']);
        });
    }
}
