<?php

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

class ChangeAuthoritiesInventory extends Migration
{
    protected $changeNames=[
        ["库存管理-动库报表","库存管理-库存"],
    ];
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //修改或没有则添加
        foreach ($this->changeNames as $namePack){
            $authority=Authority::where('name',$namePack[0])->orWhere('name',$namePack[1])->first();
            if(!$authority){
                (new Authority(['name'=>$namePack[1],'alias_name'=>$namePack[1]]))->save();
            }elseif($authority['name']==$namePack[0]){
                $authority['name']=$namePack[1];
                $authority['alias_name']=$namePack[1];
                $authority->save();
            }
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //改回旧的
        foreach ($this->changeNames as $namePack){
            $authority=Authority::where('name',$namePack[1])->first();
            $authority['name']=$namePack[0];
            $authority->save();
        }
    }
}
