Laravel 5:Model.php 中的 MassAssignmentException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34565515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:04:52  来源:igfitidea点击:

Laravel 5 : MassAssignmentException in Model.php

phplaravellaravel-5eloquentmass-assignment

提问by Sandeep

I am getting this error:

我收到此错误:

MassAssignmentException in Model.php line 448: _token

Model.php 第 448 行中的 MassAssignmentException:_token

When I am using createmethod. Please review code below:

当我使用create方法时。请查看以下代码:

Contacts.php(Model):

Contacts.php(模型):

class Contacts extends Model
{
    protected $table = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at'];
}

ContactsController.php(Controller):

ContactsController.php(控制器):

public function store(Request $request)
{        
    $inputs = $request->all();
    $contacts = Contacts::Create($inputs);
    return redirect()->route('contacts.index');
}

回答by Moppo

For the Mass Assignment Exception: you should specify all the fields of the model that you want to be mass-assignable through create or update operations on the property $fillable:

对于批量分配异常:您应该通过属性上的创建或更新操作指定要批量分配的模型的所有字段$fillable

protected $fillable = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at'];

Besides, the field $tableshould contain only the model's table name:

此外,该字段$table应仅包含模型的表名:

protected $table = 'your_table_name';

回答by shakee93

This might happen in case if you have used the wrongly imported the class. if you are using the User Model.

如果您使用了错误导入的类,则可能会发生这种情况。如果您使用的是用户模型。

Wrong Import

错误的导入

// mostly IDE suggestion
use Illuminate\Foundation\Auth\User;

Correct Model Import

正确的模型导入

use App\User;

i have gone through this. might help someone.

我经历过这个。可能会帮助某人。

回答by Ferhat KO?ER

You can all column fillable:

您可以将所有列填充:

protected $guarded = array();

Add your model.

添加您的模型。

回答by Youssef Belyazidi

You need only to add the following to your Model (Contact):

您只需将以下内容添加到您的模型 ( Contact) 中:

protected $fillable = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at'];

For example:

例如:

class Contacts extends Model { 
   protected $table = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at']; 
   protected $fillable = [ 'name', 'mobile', 'email', 'address', 'created_at', 'updated_at' ]; 
}

回答by Joynal Abedin

If all of the above fails, you can try following.

Put following after namespace.

如果以上都失败了,您可以尝试以下操作。

将以下内容放在命名空间之后。

use Eloquent;

Put following at the start of your storemethod.

将以下内容放在store方法的开头。

Eloquent::unguard();

like:

喜欢:

public function store(Request $request)
{        
   Eloquent::unguard();
   $inputs = $request->all();
   $contacts = Contacts::Create($inputs);
   return redirect()->route('contacts.index');
}

This is not recommended though, as this makes things vulnerable to attacks. But if you need a quick fix this might help.

但是,不建议这样做,因为这会使事情容易受到攻击。但是,如果您需要快速修复,这可能会有所帮助。

回答by Rahul Hirve

Check Model you have Imported or Not. If not then use this.

检查您是否已导入的模型。如果没有,那么使用这个。

<?php 

 namespace App\Http\Controllers\Auth; 
 use App\Http\Controllers\Controller; 
 use App\User;

回答by Keith Turkowski

Make sure you are putting the $fillable or $guarded in the app\Contacts.php file and not the app\Http\Controllers\ContactsController.php file. It should be obvious, but it can be overlooked.

确保将 $fillable 或 $guarded 放在 app\Contacts.php 文件中,而不是 app\Http\Controllers\ContactsController.php 文件中。它应该是显而易见的,但它可以被忽略。