Laravel 5 和委托。如何同时保存用户和附加角色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29998135/
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-09-14 11:28:44  来源:igfitidea点击:

Laravel 5 and Entrust. How to save user and attach role at the same time

phplaravelweblaravel-5user-roles

提问by Nick

Has anyone tried Entrust for User Roles & Permissions in Laravel 5?

有没有人在 Laravel 5 中尝试过用户角色和权限的委托?

I want to add and save user and attach role into it at the same time. here's my code

我想同时添加和保存用户并附加角色。这是我的代码

     $role = Role::where('name','=','admin')->first();
     $user = new User();
     $user->name = Input::get('name');
     $user->email = Input::get('email');
     $user->password = Hash::make(Input::get('password'));
      if($user->save()){
          $user->attachRole($role);
          return redirect('dashboard/users')->with('success-message','New user has been added');
      }

But $user->attachRole($role);won't work though it works on my databaseSeederbut not on my UserController.

$user->attachRole($role);不会工作,虽然它适用于我的databaseSeeder但不适用于我的 UserController。

回答by Harry Geo

I think you have this problem because you never save in the DB. Try something like this.

我认为你有这个问题,因为你从来没有保存在数据库中。尝试这样的事情。

 $role = Role::where('name','=','admin')->first();
 $user = new User();
 $user->name = Input::get('name');
 $user->email = Input::get('email');
 $user->password = Hash::make(Input::get('password'));
 $user->save()
 $user->attachRole($role);
 return redirect('dashboard/users')->with('success-message','New user has been added');

Of course this method will work only if you auto-increment your model's id using laravel's auto-increment feature. If you are using something like uuids to uniquely identify your fields, you should also include public $incrementing = false;inside your Model.

当然,只有当您使用 laravel 的自动递增功能自动递增模型的 id 时,此方法才有效。如果您使用 uuid 之类的东西来唯一标识您的字段,您还应该将其包含public $incrementing = false;在您的模型中。

Make sure you include the HasRole trait inside your model.

确保在模型中包含 HasRole 特征。

Also take a look at Akarun's answer as it will also work.The create method, create's a new instance and also save to db so you don't need to $user->save()

还可以看看 Akarun 的答案,因为它也可以工作。 create 方法,创建一个新实例并保存到 db 所以你不需要 $user->save()

回答by Akarun

I also use "Entrust" for managing my user permissions, but I use createsyntax to store my User. Then I use "roles()->attach", like this :

我也使用“Entrust”来管理我的用户权限,但我使用create语法来存储我的用户。然后我使用“roles()->attach”,像这样:

$user = User::create($this->userInputs($request));
$user->roles()->attach($request->input('role'));

回答by DonnaJo

I am using the provided Auth setup that ships with Laravel 5 with my own adjustments, so I just do the following in Registrar.php:

我正在使用 Laravel 5 附带的提供的 Auth 设置和我自己的调整,所以我只在 Registrar.php 中执行以下操作:

public function create( array $data )
{
    // Create a new user, and assign it to 'new_user'
    $new_user = User::create( [
        'username'  => $data['username'], //<< Specific to my own db setup
        'email'     => $data['email'],
        'password'  => bcrypt( $data['password'] ),
    ] );

    // Initiate the 'member' Role
    $member = Role::where( 'name', '=', 'member' )->first();
    // Give each new user the role of 'member'
    $new_user->attachRole( $member );

    // Return the new user with member role attached
    return $new_user; //<<Or whatever you do next with your new user
}

You just have to be sure to use App\Role at the top of the file. Works great for me.

您只需要确保在文件顶部使用 App\Role。对我很有用。