<?php

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

class DropCommodityBarcodeColumnToBarcodeTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
//        \App\Commodity::query()->count();
//        DB::insert('INSERT commodity_barcodes (code,commodity_id) SELECT barcode, id FROM commodities');
//        $this->delRepeated();
        Schema::table('commodities', function (Blueprint $table) {
            $table->dropColumn('barcode');
        });
    }

//    private function delRepeated(){
//        $codes=CommodityBarcode::groupBy('code','commodity_id')->havingRaw('count(*) > ?', [1])->get();
//        $codes->each(function(CommodityBarcode $code){
//            $delCodes=CommodityBarcode::where('code',$code['code'])->where('commodity_id',$code['commodity_id'])->where('id','!=',$code['id'])->get();
//            $delCodes->each(function(CommodityBarcode $delcode){
//                $delcode->delete();
//            });
//        });
//    }

}
