如何解决 laravel 中的质量分配错误?
How to resolve mass assignment error in laravel?
我是 laravel 的新手,我正在实施用户注册 api,但在注册用户时会抛出类似 mass_assignment
的错误
Add [first_name] to fillable property to allow mass assignment on [App\Entities\User].
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table ='user';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'first_name','last_name','phone_number','email'
];
protected $guarded =[];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
UserController.php
public function userRegister(userRequest $request){
$data = $this->repository->create($request->all());
return $user = new UserTransformer($data);
}
}
请帮忙解释为什么会发生这个错误..?
主要问题是laravel5.8模型路径不同所以使用下面的facade
use App\modelname
而不是使用 App\Entities\User,如果这能解决您的问题,请告诉我:-)
我是 laravel 的新手,我正在实施用户注册 api,但在注册用户时会抛出类似 mass_assignment
的错误Add [first_name] to fillable property to allow mass assignment on [App\Entities\User].
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table ='user';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'first_name','last_name','phone_number','email'
];
protected $guarded =[];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
UserController.php
public function userRegister(userRequest $request){
$data = $this->repository->create($request->all());
return $user = new UserTransformer($data);
}
}
请帮忙解释为什么会发生这个错误..?
主要问题是laravel5.8模型路径不同所以使用下面的facade
use App\modelname
而不是使用 App\Entities\User,如果这能解决您的问题,请告诉我:-)