Laravel 5.3 - Best way to implement Entrust role on signup? -
i'm working laravel 5.3
, i'm trying set role when signs up, i've used zizaco entrust
library.
i'm unsure on best way achieve this.
i tried inside registercontroller
's create
method below:
protected function create(array $data) { return user::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); $user = user::where('email', '=', $data['email'])->first(); // role attach alias $user->attachrole($employee); }
but that's not right. i'm bit unsure on best practice sort of thing.
if, comment on op suggests, want assign same role registered user, can use model observer - it's simple.
// app/observers/userobserver.php <?php namespace app\observers; use app\models\user; use app\models\role; // or namespace zizaco role class class userobserver { public function created( user $user ) { $role = role::find( 1 ); // or other way of getting role $user->attachrole( $role ); }
then register observer in appserviceprovider:
// app/providers/appserviceprovider.php use app\models\user; use app\observers\userobserver; class appserviceprovider extends provider { public function boot() { user::observe( new userobserver ); // ... } // ... }
Comments
Post a Comment