我正在试图确定哪种播种与我是否将模型扩展到基本模型有关或不扩展,因为当我将用户模型转换为扩展雄辩并运行种子文件时它可以工作但我保持原样,然后为所有可填写字段添加空值。
关于为什么会发生这种情况以及如何解决这个问题的想法?
用户模型
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends BaseModel implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait, SoftDeletingTrait;
protected $fillable = ['first_name', 'last_name', 'username', 'avatar', 'role_id', 'status_id', 'email_address', 'password'];
protected static $rules = [
'first_name' => 'required',
'last_name' => 'required',
'username' => 'required|unique:users',
'email_address' => 'required|email|unique:users',
'avatar' => 'required|unique:users',
'password' => 'required|confirmed',
'role_id' => 'required',
'status_id' => 'required'
];
public function __construct()
{
parent::__construct();
}
/**
* 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');
/**
* Get the user's full name by concatenating the first and last names
*
* @return string
*/
public function getFullName()
{
return $this->first_name . ' ' . $this->last_name;
}
}
基本型号
<?php
class BaseModel extends Eloquent {
protected $errors;
public function __construct()
{
parent::__construct();
}
public static function boot()
{
parent::boot();
static::saving(function($model)
{
return $model->validate();
});
}
public function validate()
{
$validation = Validator::make($this->getAttributes(), static::$rules);
if ($validation->fails())
{
$this->errors = $validation->messages();
return false;
}
return true;
}
public function getErrors()
{
return $this->errors;
}
}
UsersTableSeeder
<?php
// Composer: "fzaninotto/faker": "v1.3.0"
use Faker\Factory as Faker;
class UsersTableSeeder extends Seeder {
public function run()
{
User::create([
'first_name' => 'First',
'last_name' => 'Last',
'username' => 'myusername',
'email_address' => 'myemail@gmail.com',
'avatar' => 'Myava',
'password' => Hash::make('changeme'),
'role_id' => 4,
'status_id' => 1
]);
//create instanances for each fake user
$faker = Faker::create();
$roleIds = UserRole::lists('id');
$statusIds = UserStatus::lists('id');
foreach(range(1, 20) as $index)
{
User::create([
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'username' => $faker->username,
'email_address' => $faker->freeEmail,
'avatar' => $faker->lexify('????????'),
'password' => Hash::make($faker->word),
'role_id' => $faker->randomElement($roleIds),
'status_id' => $faker->randomElement($statusIds)
]);
}
}
}
答案 0 :(得分:0)
你的构造函数与Eloquent的不兼容。
摆脱它们,因为它们什么也不做,或者兼容:
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
}