简体   繁体   English

Laravel 5.4 updateorcreate中的MassAssignmentException

[英]MassAssignmentException in updateorcreate in laravel 5.4

I am newbie to laravel I am using updateorcreate for model. 我是laravel的新手,正在为模型使用updateorcreate。 But this is showing error MassAssignmentException end_time In Model Tasktimelog I am using protected $guarded = array(); 但是,这显示了错误MassAssignmentException end_time在Model Tasktimelog中,我正在使用受保护的$ guarded = array();。 Here is what I am Doing. 这是我在做什么。

$endtask= Tasktimelog::updateOrCreate(
        [
            'task_id' => $taskid, 
            'action_type'=>4, 
            'user_id'=> auth()->id()
        ],
        [
          'end_time'             => $endtimeis,
          'total_time'           => request('totalseconds'), 
          'remark'               => request('remark'), 
          'actual_complete_time' => $diff, 
          'project_id'           => $getprojectid->project_id
        ]);

Laravel doesn't let you to massive update fields of your database if you don't specificate it: 如果不指定参数,Laravel不允许您大规模更新数据库的字段:

What you have to do is add the $fillable variable to your model with the fields that can be massively updated: 您需要做的是将$ fillable变量添加到模型中,其中包含可以大量更新的字段:

class Tasktimelog extends Model
{
    protected $fillable = ['task_id', 'action_type', 'user_id']; //and the rest of your fields
}

Laravel Doc: While $fillable serves as a "white list" of attributes that should be mass assignable, you may also choose to use $guarded. Laravel Doc:虽然$ fillable用作应该批量分配的属性的“白名单”,但您也可以选择使用$ guarded。 The $guarded property should contain an array of attributes that you do not want to be mass assignable. $ guarded属性应包含您不想大量分配的属性数组。

https://laravel.com/docs/5.5/eloquent#eloquent-model-conventions https://laravel.com/docs/5.5/eloquent#eloquent-model-conventions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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