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!
{{ Form::select('role[]', $roles , Input::old('role'), array('class' => 'select', 'multiple')) }}
important[]
– Amit Garg