向用户 Eloquent ORM 模型添加字段

Adding fields to User Eloquent ORM model

我想扩展 Laravel 5.0 中的现有用户模型以向 table 添加新列。我该怎么做?

  1. 通过 运行 命令创建 migration
php artisan make:migration users_disabled_column

其中 disabled 是您要添加到现有的列的名称 table。

  1. 使用 adding column 编辑新迁移,示例如下:
<?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');
        });
    }
}
  1. 执行创建的迁移:

php artisan migrate

  1. 现在您可以使用新列了:
$user = User::find($id);
$user->disabled = false;
$user->save();