osb/database/migrations/2017_12_06_000001_CreateTableCountries.php

60 lines
1.3 KiB
PHP
Raw Normal View History

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Models\{Country,Currency};
class CreateTableCountries extends Migration
{
private $convert = 'App\Models\Old\Country';
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::dropIfExists('countries');
Schema::create('countries', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('twocode',2)->nullable();
$table->string('threecode',3);
$table->integer('currency_id')->unsigned()->nullable();
$table->boolean('active');
$table->foreign('currency_id')->references('id')->on('currencies');
});
if ($this->convert)
foreach (($this->convert)::all() as $o)
{
if ($o->currency)
$co = Currency::where('name',$o->currency->name)->firstOrFail();
$oo = new Country;
$oo->name = $o->name;
$oo->twocode = $o->two_code;
$oo->threecode = $o->three_code;
$oo->active = $o->three_code == 'AUS' ? TRUE : FALSE;
if ($o->currency)
$co->countries()->save($oo);
else
$oo->save();
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('countries');
}
}