Jump to content

Laravel 5.4 attaching role on registration

Joveice

Hello.

 

I'm trying to attach roles to my users when registrating

 

Role.php

class Role extends Model
{
    public function users()
    {
        return $this->belongsToMany('App\User', 'role_users', 'role_id', 'user_id');
    }
}

In User.php

public function roles()
    {
        return $this->belongsToMany('App\Role', 'role_users', 'user_id', 'role_id');
    }

My roles

id			slug				name
1			admin				Administrator

2			default				Member

So in my registerController I got this

/**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => $data['password'],
            'gender' => $data['gender'],
            'country' => $data['country'],
            'birthday' => $data['birthday'],
        ]);
        $role = new Role(['slug' => 'default']);
        $user->roles()->save($role);
        return $user;
    }

But this throws 

Illuminate \ Database \ Eloquent \ MassAssignmentException
slug

I'm not sure that my registerController is close to correct as I saw thats how someone else did it and I just had to try. The other way I saw someone do was this

/**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => $data['password'],
            'gender' => $data['gender'],
            'country' => $data['country'],
            'birthday' => $data['birthday'],
        ]);
		$role = Role::where('name', 'Member')->first();
		$user->save();
		$user->roles()->attach($role);
    }

How do you do this? If you need more info from me let me know as I'm new to laravel :)

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Hi, im still a beginer with the Laravel framework but the exception

Illuminate \ Database \ Eloquent \ MassAssignmentException
slug

happen, I think, when you do mass assignment on a field. Its a protection implanted so the users can't update data when you don't want to. You have to manually define which fields could be "mass assigned" :

class User extends Model
{
    protected $fillable = ['name', 'email', 'user', 'gender'];
}

I wouldn't put the password. Reed more about it here : http://laravel.com/docs/eloquent#mass-assignment

 

Sory for my english, still learning ;)

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

You are creating a new role in your registerController every time someones registers, and i guess you don't have the fillable array on the Roles model. You need to add the slug to the fillable array on your Role model and instead of creating a new Role every time someones register you should create the default role in the database before that and assign that one to the user when they register.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×