【发布时间】:2017-12-24 02:11:55
【问题描述】:
在我的项目中,我拥有角色和权限,并且我为我的用户创建了一个控制器,以便他们可以编辑他们的个人资料,因为 usercontroller 仅限于管理员角色。
现在我的问题是,当用户更新他的个人资料时,他的角色访问权限将被分离。 我应该怎么做才能让用户能够在不更改角色的情况下编辑自己的个人资料?
这是我的更新函数:
public function update(Request $request, $id)
{
$user = User::findOrFail($id); //Get role specified by id
//Validate name, email and password fields
$this->validate($request, [
'name'=>'required|max:120',
'username'=>'required|max:120',
'email'=>'required|email|unique:users,email,'.$id,
'password' => 'nullable|string|min:6|confirmed'
]);
$input = $request->only(['name', 'username', 'email']); //Retreive the name, email and password fields
if (trim(Input::get('password')) != '') {
$user->password = Hash::make(trim(Input::get('password')));
}
$user->fill($input)->save();
return view('users.profile')
->with('flash_message',
'Your Profile successfully edited.');
}
用户模型:
protected $fillable = [
'name', 'username', 'email', 'password', 'affiliate_id', 'referred_by',
];
数据库:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
$table->string('referred_by')->nullable();
$table->string('affiliate_id')->unique();
$table->rememberToken();
$table->timestamps();
});
}
【问题讨论】:
-
when user update his profile his role access will be detach是什么意思? -
角色是如何附加到用户的?它是用户模型上的属性吗?连接表?
-
@tompec 这意味着当我的用户在保存时编辑他们的用户名等信息时,他们将失去他们的角色,他们在数据库中的角色列变为空
-
@Cryode 我更新了我的问题,我在数据库一中使用中间件和表作为角色一的权限。
-
您能否从会话 或 db 中检索他们现有的用户值,将其添加到 $input 然后重新保存?这是你调用
fill的$user对象,为什么不直接在用户对象上操作,或者使用fill之外的东西