Skip to content

Instantly share code, notes, and snippets.

@armincifuentes
Created May 13, 2015 20:51
Show Gist options
  • Save armincifuentes/0c68c62fe2920f4d2725 to your computer and use it in GitHub Desktop.
Save armincifuentes/0c68c62fe2920f4d2725 to your computer and use it in GitHub Desktop.
Non-destructive, conditional migration for Laravel
<?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