php - Laravel Eloquent Save to DB Using Create - Unhelpful Error -
i unhelpful error when try , insert new record in db. have idea start solve error?
user_id that's says. (this name of 1 of required fields in table i'm saving it's not descriptive.)
for reference here's code:
$data = array( 'user_id'=>1, 'post_type'=>'0', 'post_title'=>'blah', 'description'=>'blah'); //it fails on line $id = post::create($data); here's model looks like:
class post extends eloquent{ public static $rules = array( 'user_id'=>'required', 'post_type'=>'required', 'post_title' => 'required|max:25', 'description'=>'required' ); public static function validate($data){ return validator::make($data, static::$rules); } public function user(){ return $this->hasone('user', 'id', 'user_id'); } } tried getting rid of relationships , validation in model. didn't work.
this called mass-assignment in laravel, need working on model set protected array called $guarded empty. add model.
protected $guarded = array(); what array does, can prevent attributes mass-assigned array, if don't want attribute filled model::create() method, need specify attribute in $guarded array.
if instead want specify fillable attributes, laravel's eloquent provides array called $fillable specify attributes want fill via mass-assigment way.
further reading:
mass assignment: http://laravel.com/docs/eloquent#mass-assignment
create method: http://laravel.com/docs/eloquent#insert-update-delete
Comments
Post a Comment