我想在Laravel 5.0中扩展现有的User模型,以向表中添加新列。我怎么能这样做?
答案 0 :(得分:4)
php artisan make:migration users_disabled_column
其中disabled
是您要添加到现有表格的列名称。
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UsersDisabledColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function($table) {
$table->boolean('disabled')->default(false);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('disabled');
});
}
}
php artisan migrate
$user = User::find($id);
$user->disabled = false;
$user->save();