<?php

use App\Models\Pracovisko\Pracovisko;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

/**
 * Class AddEmailToPracoviskaTable
 */
class AddEmailToPracoviskaTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('pracoviska', function (Blueprint $table) {
            $table->string('email')->nullable()->after('nazov');
        });

        Pracovisko::query()->get()->each(function (Pracovisko $pracovisko) {
            $pracovisko->email = sprintf('%s@sekait.sk', $pracovisko->kod);
            $pracovisko->save();
        });
    }

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