播种时使用未定义的常数

时间:2014-08-23 19:50:02

标签: php laravel laravel-4

我尝试使用 php artisan db:seed 播种数据库时出现此错误:

[ErrorException] Use of undefined constant username - assumed 'username'

我已经检查了Laravel文档,语法看起来是正确的,但是我不确定我在搞乱这个问题。

<?php

class DatabaseSeeder extends Seeder {

/**
 * Run the database seeds.
 *
 * @return void
 */
public function run(){
    Eloquent::unguard();

    $this->call('UsersTableSeeder');
  }

}


class UsersTableSeeder extends Seeder {

public function run(){
    User::create([
        'username'  => 'Ziggy',
        'email'     => 'ziggy@stardust.com',
        'password'  =>  '1234'
    ]);
  }
}

编辑:我添加了用户模型:     

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    public $timestamps = false;
    protected $fillable = [username, password];

    use UserTrait, RemindableTrait;

    /**
    * The database table used by the model.
    *
    * @var string
    */
    protected $table = 'users';

    /**
    * The attributes excluded from the model's JSON form.
    *
    * @var array
    */
    protected $hidden = array('password', 'remember_token');

}

以下是使用的迁移文件:

<?php

   use Illuminate\Database\Schema\Blueprint;
   use Illuminate\Database\Migrations\Migration;

   class CreateUsersTable extends Migration {

   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   {
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('username');
        $table->string('email')->unique();
        $table->string('password');
        $table->timestamps();
    });
  }

   /**
   * Reverse the migrations.
   *
   * @return void
   */
   public function down()
   {
       Schema::table('users', function(Blueprint $table)
       {
            Schema::drop('users');
        });
    }

}

1 个答案:

答案 0 :(得分:2)

在User类中,更改:

protected $fillable = [username, password];

到这个

protected $fillable = ['username', 'password'];

声明它的方式,PHP将它们作为常量而不是字符串