php - Extending Models and Seeding Tables with Eloquent in Laravel -
i'm trying figure seeding has whether or not extend model base model extends eloquent or not because when turn user model extending eloquent , run seed file works keep way puts empty values fillable fields.
any ideas why happening , how 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(); } /** * database table used model. * * @var string */ protected $table = 'users'; /** * attributes excluded model's json form. * * @var array */ protected $hidden = array('password', 'remember_token'); /** * user's full name concatenating first , 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 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 each fake user $faker = faker::create(); $roleids = userrole::lists('id'); $statusids = userstatus::lists('id'); foreach(range(1, 20) $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 incompatible eloquent's.
get rid of them, since don't anything, or make compatible:
public function __construct(array $attributes = []) { parent::__construct($attributes); }
Comments
Post a Comment