当尝试使用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' => '[email protected]',
'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');
});
}
}
在用户类中,更改以下内容:
protected $fillable = [username, password];
对此
protected $fillable = ['username', 'password'];
声明方式,PHP将它们视为常量而不是字符串
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句