简体   繁体   中英

Extending Models and Seeding Tables with Eloquent in Laravel

I'm trying to figure what seeding has to do with whether or not I extend a model to the base model which extends eloquent or not because when I turn the user model into extending eloquent and run a seed file it works but I keep it the way it is then it puts empty values for all the fillable fields.

Any ideas as to why this is happening and how to possibly fix it?

User Model

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

Base Model

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

Your constructors are incompatible with Eloquent's.

Get rid of them, since they don't do anything, or make compatible:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM