扩展模型和播种表与雄辩的拉拉维尔


Extending Models and Seeding Tables with Eloquent in Laravel

我试图弄清楚种子设定与我是否将模型扩展到扩展雄辩的基本模型有什么关系,因为当我将用户模型转换为扩展雄辩并运行种子文件时,它可以工作,但我保持原样,然后它为所有可填写字段放置空值。

关于为什么会发生这种情况以及如何解决它的任何想法?

用户模型

<?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)
            ]);
        }
    }
}

你的构造函数与 Eloquent 的不兼容。

摆脱它们,因为它们不做任何事情,或者兼容:

public function __construct(array $attributes = [])
{
    parent::__construct($attributes);
}