<?php

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

use App\Models\Contentable;

class AddModalToExpandable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $contentables = Contentable::all()->pluck('expandable', 'id')->toArray();

        Schema::table('contentables', function (Blueprint $table) {
            $table->dropColumn('expandable');
        });

        Schema::table('contentables', function (Blueprint $table) {
            $table->string('expandable')->nullable();
        });

        foreach ($contentables as $id => $expanded) {
            $contentable = Contentable::findOrFail($id);
            $contentable->expandable = $expanded ? 'expand' : null;
            $contentable->save();
        }
    }

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