【问题标题】:Extending Models and Seeding Tables with Eloquent in Laravel在 Laravel 中使用 Eloquent 扩展模型和播种表
【发布时间】:2015-01-10 00:22:13
【问题描述】:

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

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

用户模型

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

【问题讨论】:

    标签: php laravel eloquent models


    【解决方案1】:

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

    摆脱它们,因为它们什么都不做,或者兼容:

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

    【讨论】:

    • 你是说我需要把它放在用户模型和 BaseModel 中吗?
    • 您应该删除那些构造函数,除非您将添加依赖项,我对此表示怀疑。是的,如果你想要构造函数,那么两者都应该这样看。但同样,在这种形式下,它们是多余的
    猜你喜欢
    • 1970-01-01
    • 2020-06-19
    • 2016-07-19
    • 2015-03-25
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多