Created
May 13, 2015 20:51
-
-
Save armincifuentes/0c68c62fe2920f4d2725 to your computer and use it in GitHub Desktop.
Non-destructive, conditional migration for Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class Zones extends Migration { | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
if ( Schema::hasTable('zones') ) : | |
Schema::table('zones', function($table) { | |
$this->_zones($table); | |
}); | |
else: | |
Schema::create('zones', function($table) { | |
$this->_zones($table); | |
}); | |
endif; | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('zones'); | |
} | |
private function _zones($table) { | |
$table->increments('id'); | |
$table->string('code')->index(); | |
$table->string('name'); | |
$table->integer('parent_id')->unsigned(); | |
$table->foreign('parent_id')->references('id')->on('zones'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment