我正在建立一个只有管理员可以添加用户的系统。 我有一个问题,因为填写表格后我收到的错误看起来像: Model.php第225行中的MassAssignmentException: _token
我的表格:
{!! Form::open(['url'=>'adminpanel/adduser/store','class'=>'form-horizontal']) !!}
<div class="form-group">
<div class="col-md-4 control-label">
{!! Form::label('nameandsurname','imie i nazwisko:') !!}
</div>
<div class="col-md-6">
{!! Form::text('nameandsurname', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-4 control-label">
{!! Form::label('PESEL','PESEL:') !!}
</div>
<div class="col-md-6">
{!! Form::text('PESEL',null,['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-4 control-label">
{!! Form::label('adress','adres:') !!}
</div>
<div class="col-md-6">
{!! Form::text('adress',null,['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-4 control-label">
{!! Form::label('position','stanowisko:') !!}
</div>
<div class="col-md-6">
{!! Form::text('position',null,['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-4 control-label">
{!! Form::label('email','email:') !!}
</div>
<div class="col-md-6">
{!! Form::text('email',null,['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-4 control-label">
{!! Form::label('leavesdays','liczbadniwolnych:') !!}
</div>
<div class="col-md-6">
{!! Form::text('leavesdays',null,['class'=>'form-control']) !!}
</div>
</div>
{!! Form::hidden('password', null) !!}
{!! Form::hidden('remember_token',null) !!}
这是用户模型:
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'nameandsurname', 'email','PESEL','adress','position','leavesdays',
];
protected $table = 'users';
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
控制器中的请求:
public function store(AddUserRequest $request)
{
// User::create($request->except('_token'));
$newUser = User::create($request->all());
$newUser->save();
return redirect('/adminpanel/admindashboard');
}
迁移:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('nameandsurname',40);
$table->string('PESEL',9);
$table->string('adress',30);
$table->enum('position',array('employer','worker','accountant'));
$table->string('email',100)->unique();
$table->string('password');
$table->integer('leavesdays');
$table->rememberToken();
$table->timestamps();
});
}
我在互联网上寻找这个问题,但任何建议都对我有帮助。
答案 0 :(得分:0)
为了使用Laravel的质量分配功能,您必须在create()
方法上提供一个数组,如
User::create(['nameandsurname'=>'some_value', 'email'=>'sampleEmail@mil.com']...etc);
请注意,如果您提供未在$fillable
中定义的字段,您也会收到例外情况。有关更多信息,请查看文档https://laravel.com/docs/5.4/eloquent#mass-assignment
答案 1 :(得分:0)
$fillable
告诉Elequent模型构造函数的预期参数。所以,最佳做法是将User
创建为
$request_data = $request->only(['nameandsurname', 'email','PESEL','adress','position','leavesdays']);
$user = User::create($request_data);
答案 2 :(得分:0)
也许代替User::create($request->all());
你可以做这样的事情:
User::create($request->only((new User())->getFillable()));
使用getFillable,您将获得protected $fillable
变量中的所有字段,而$request->only($fields)
只会提供这些字段