user13777183
user13777183

Reputation:

Laravel Attach Role using Select Box

i'm trying to attach role for user using select box but it doesn't work

i've Roles table and bridge table between Roles and Users Table

i know it's many-to-many relationship and i should use check boxes instead of select box

but at the time for some reason i need to assign only 1 Role for user

So, i have to use select box

Any ideas?

Controller

public function store(Request $request)
{
    $this->validate($request, array(
        'name'         => 'required|string|max:255',
        'email'          => 'required|string|email|max:255|unique:users',
        'password'          => 'required|string|min:8',
    ));
    $password = Hash::make($request->password);
    $user = new User;
    $user->name = $request->input('name');
    $user->email = $request->input('email');
    $user->password = $password;
    $user->save();

    if ($request['user']) {
        $user->roles()->attach(Role::where('name','user')->first());
    }
    if ($request['editor']) {
        $user->roles()->attach(Role::where('name','editor')->first());
    }
    if ($request['admin']) {
        $user->roles()->attach(Role::where('name','admin')->first());
    }

    return redirect('users')->with('success', 'user is successfully saved');
}

View

                        <div class="form-group">
                            <label class="col-12">Roles</label>
                            <div class="col-12">
                                <div class="custom-control custom-radio mb-5">
                                    <input class="custom-control-input" type="radio" name=admin" id="example-radio1" value="admin">
                                    <label class="custom-control-label" for="example-radio1">Admin</label>
                                </div>
                                <div class="custom-control custom-radio mb-5">
                                    <input class="custom-control-input" type="radio" name=pharmacy" id="example-radio2" value="editor">
                                    <label class="custom-control-label" for="example-radio2">Editor</label>
                                </div>
                                <div class="custom-control custom-radio mb-5">
                                    <input class="custom-control-input" type="radio" name=company" id="example-radio3" value="user">
                                    <label class="custom-control-label" for="example-radio3">User</label>
                                </div>
                            </div>
                        </div>

Upvotes: 0

Views: 255

Answers (1)

lagbox
lagbox

Reputation: 50511

I think checkboxes might be better here instead of individual radio inputs.

<input type="checkbox" name="roles[]" value="admin"><label>Admin</label>
<input type="checkbox" name="roles[]" value="editor"><label>Editor</label>
<input type="checkbox" name="roles[]" value="user"><label>User</label>

On the Laravel side:

// find the ids of the roles submitted by name
$role_ids = Role::whereIn('name', $request->input('roles', []))->pluck('id');
// attach any ids needed
$user->roles()->attach($role_ids);

Upvotes: 0

Related Questions