If you're trying to modify database table
, use following code for migrations:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class [class_name] extends Migration
{
public function table()
{
return '[table_name]';
}
public function columns()
{
return [
[
"type"=>"integer",
"column"=>"[column_name]",
"after"=>"[after_column_name] (optional)",
]
];
}
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$table = $this->table();
$list = $this->columns();
foreach ($list as $item)
{
if(!Schema::hasColumn($table, $item['column'])){
Schema::table($table, function (Blueprint $table) use ($item)
{
$type = $item['type'];
$column = $item['column'];
if(isset($item['after']))
{
$table->$type($column)->nullable()->after($item['after']);
} else
{
$table->$type($column)->nullable();
}
});
}
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$table = $this->table();
$list = $this->columns();
foreach ($list as $item)
{
if(Schema::hasColumn($table, $item['column'])){
Schema::table($table, function (Blueprint $table) use ($item)
{
$column = $item['column'];
$table->dropColumn($column);
});
}
}
}
}
Top comments (0)