<?php

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

class AddModelToService extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('ab_service', function (Blueprint $table) {
            $table->string('model')->nullable();
        });

        // Go through our services and add the relation
		foreach (\App\Models\Service\Adsl::all() as $o)
			$this->update($o);
		foreach (\App\Models\Service\Domain::all() as $o)
			$this->update($o);
		foreach (\App\Models\Service\Host::all() as $o)
			$this->update($o);
		foreach (\App\Models\Service\SSL::all() as $o)
			$this->update($o);
		foreach (\App\Models\Service\Voip::all() as $o)
			$this->update($o);
    }

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

    function update(\Illuminate\Database\Eloquent\Model $o)
	{
		$oo = \App\Models\Service::findOrFail($o->service_id);
		$oo->model = get_class($o);
		$oo->save();
	}
}
