<?php

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

class ChangeLaborReportsLaborCompanyToLaborCompanyId extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('labor_reports', function (Blueprint $table) {
            $table->bigInteger('labor_company_id')->nullable()->index()->comment('劳务所');
        });
        $laborReports=\App\LaborReport::query()->get();
        foreach ($laborReports as $laborReport){
            $laborCompanyId=\App\LaborCompany::query()->where('name',$laborReport->labor_company)->value('id');
            $laborReport->update([
                $laborReport->labor_company_id=$laborCompanyId
            ]);
        }
        Schema::table('labor_reports', function (Blueprint $table) {
            $table->dropColumn('labor_company');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('labor_reports', function (Blueprint $table) {
            $table->string('labor_company')->nullable()->comment('劳务所');
        });
        $laborReports=\App\LaborReport::query()->get();
        foreach ($laborReports as $laborReport){
            $laborCompanyName=\App\LaborCompany::query()->where('id',$laborReport->labor_company_id)->value('name');
            $laborReport->update([
                $laborReport->labor_company=$laborCompanyName
            ]);
        }
        Schema::table('labor_reports', function (Blueprint $table) {
            $table->dropColumn('labor_company_id');
        });
    }
}
