1
votes

Hi I am using Laravel and I am using a couple of packages for user auth and roles which are Zizaco Confide and Zizaco Entrust. I have run through the setup process and I have roles and permissions. This has created the following tables:

users, roles, assigned_roles, permissions, permission_role

The structure of the tables is below with two users:

users table
id | username    | email 
1  | adminuser   | [email protected]
2  | subscriber  | [email protected]

roles
id | name 
1  | admin
2  | subscriber

assigned_roles
id | user_id | role_id
1  | 1       | 1
2  | 1       | 2
3  | 2       | 2

permissions
id | name              | display_name
1  | can_view_admin    | Can View Admin Display
2  | can_view_articles | Can View Articles

permission_role
id | permission_id | role_id 
1  | 1             | 1
2  | 1             | 2
3  | 2             | 2

I am using the standard controller suppled by confide to add a user however I have added a few lines to the controller and sign up form (custom views I am using, published the config files and edited in there) as so:

public function create()
    {

        $roles =  DB::table('roles')->orderBy('name', 'asc')->lists('name', 'id'); 
        return View::make(Config::get('confide::signup_form'),['roles' => $roles ]);
    }

This returns the roles that are in the db so that I can pass them into a multiple select as a user can have many roles. In my view i do the following:

<form method="POST" action="{{{ URL::to('users') }}}" accept-charset="UTF-8">
<input type="hidden" name="_token" value="{{{ Session::getToken() }}}">
<input class="form-control" placeholder="{{{ Lang::get('confide::confide.username') }}}" type="text" name="username" id="username" value="{{{ Input::old('username') }}}">

@if(count($roles)>0)
        {{ Form::label('select_client', 'Select Role', array('class' => 'label'));  }}
        {{ Form::select('role', $roles , Input::old('role'), array('class' => 'select', 'multiple')) }}
@endif 

Now this form is going to the standard controller supplied with the confide package which I don't mind, however this is the function:

public function store()
    {
        $repo = App::make('UserRepository');

        $user = $repo->signup(Input::all());

        if ($user->id) {
            if (Config::get('confide::signup_email')) {
                Mail::queueOn(
                    Config::get('confide::email_queue'),
                    Config::get('confide::email_account_confirmation'),
                    compact('user'),
                    function ($message) use ($user) {
                        $message
                            ->to($user->email, $user->username)
                            ->subject(Lang::get('confide::confide.email.account_confirmation.subject'));
                    }
                );
            }

            return Redirect::action('UsersController@login')
                ->with('notice', Lang::get('confide::confide.alerts.account_created'));
        } else {
            $error = $user->errors()->all(':message');

            return Redirect::action('UsersController@create')
                ->withInput(Input::except('password'))
                ->with('error', $error);
        }
    }

How can I update the assigned_roles table at the same time if it is a successful registration with the user id that has literally just been created and the role(s) id if more than one is selected? Will I need a foreach loop here? Any help would be greatly appreciated!

1
{{ Form::select('role[]', $roles , Input::old('role'), array('class' => 'select', 'multiple')) }} important []Amit Garg
@AmitGarg No that's not needed. The array notation is only necessary if you have multiple input fields with the same name. A single select doesn't need it.lukasgeiter

1 Answers

1
votes

Go into the UserRepository class and add this code to the signup() method:

$this->save($user);

$role = array_get($input, 'role');
$user->roles()->attach($role);

Now there still is a problem, because with using $this->save() the $user doesn't get the id after the insert. What you can do is either use $user->save() directly:

$user->save();
$roles = array_get($input, 'role');
$user->attachRoles($roles);

Or modify the save method in the repository to pass $user by reference: (add &)

public function save(User &$instance){
    return $instance->save()
}