[英]Laravel 5 and Entrust. How to save user and attach role at the same time
Has anyone tried Entrust for User Roles & Permissions in Laravel 5? 有没有人在Laravel 5中尝试过Entrust for User Roles&Permissions?
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);
但
$user->attachRole($role);
won't work though it works on my databaseSeeder
but not on my UserController. 虽然它可以在我的
databaseSeeder
上工作,但在我的UserController上不起作用。
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. 当然,只有使用laravel的自动增量功能自动增加模型的id时,此方法才有效。 If you are using something like uuids to uniquely identify your fields, you should also include
public $incrementing = false;
如果你使用像uuids这样的东西来唯一地标识你的字段,你还应该包括
public $incrementing = false;
inside your Model. 在你的模型中。
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的答案,因为它也会工作。创建方法,创建一个新实例并保存到db所以你不需要
$user->save()
I also use "Entrust" for managing my user permissions, but I use create
syntax to store my User. 我还使用“Entrust”来管理我的用户权限,但我使用
create
语法来存储我的用户。 Then I use "roles()->attach", like this : 然后我用“roles() - > attach”,像这样:
$user = User::create($this->userInputs($request));
$user->roles()->attach($request->input('role'));
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. 您只需确保在文件顶部使用App \\ Role。 Works great for me.
对我来说很棒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.