简体   繁体   中英

Laravel eloquent retrieve user role in multi select

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   | admin@test.com
2  | subscriber  | subscriber@test.com

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 have an edit and update function which updates the user table. However I want to also update the users permission in this view also and in the Users Controller too. But I'm not sure how to fetch the users role as they can have many roles and inject this into a multi select.

This is what I have so far:

Users Controller :

 public function edit($id)
    {
           $user = Auth::user();       
            if ($user->hasRole('Admin'))
                {
                    $user = User::find($id);
                    return View::make('users.edit')->with('user', $user);
                }
                else {
                    return \Redirect::route('article');
                }
    }



public function update($id)
    {

        $rules     = array(
            'username' => 'required',
            'email'    => 'required'
        );

        $validator = Validator::make(Input::all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::back('/admin/user/' . $id . '/edit')->withErrors($validator)->withInput();

        } else {
            // store
            $user               = User::find($id);
            $user->firstname    = Input::get('firstname');
            $user->lastname     = Input::get('lastname');
            $user->username     = Input::get('username');
            $user->email        = Input::get('email');
            $user->save();

            // redirect
            Session::flash('message', 'Successfully updated!');
            return Redirect::to('/admin');
        }
    }

My edit form is as below:

{{ HTML::ul($errors->all()) }}
{{ Form::open(array('url' => URL::to('admin/user/'.$user->id.'/update'), 'method' => 'PATCH ')) }}
{{ Form::model($user) }}

        {{ Form::text('firstname', null, array('class' => 'form-control')) }}

        {{ Form::text('lastname', null, array('class' => 'form-control')) }}

        {{ Form::text('username', null, array('class' => 'form-control')) }}

        {{ Form::text('email', null, array('class' => 'form-control')) }}


    {{ Form::submit('Update User', array('class' => 'btn btn-green')) }}
{{ Form::close() }}

I know I can fetch all the roles as so:

$roles =  DB::table('roles')->orderBy('name', 'asc')->lists('name', 'id'); 

and then insert into a multi select as so:

{{ Form::select('role', $roles , Input::old('role'), array('class' => 'select')) }}

and I know you can get the user role as so:

$roles = User::find(1)->roles();

so I tried to change this line in my edit function

$user = User::find($id);

to:

$user = User::find($id)->roles()->orderBy('name', 'asc')->lists('name', 'id');

But this throws this error:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field
 list is ambiguous (SQL: select `name`, `id` from `roles` inner join 
 `assigned_roles` on `roles`.`id` = `assigned_roles`.`role_id` where 
 `assigned_roles`.`user_id` = 1 order by `name` asc)

Does anyone know how I can fetch the users roles (as they can have more than one) using a function within entrust? Or possibly using eloquent as the data is stored in a few tables, for example in the edit screen I would want to grab the user roles by user_id this data is stored in the assigned_roles table. Then I would need to fetch the role name which is stored in the roles table and the assigned_roles table holds the role_id . I then need to put this into a multi select any ideas on how I can do that? I'm not great with eloquent so any ideas would be appreciated.

For the select, you'll need actually all roles as an array:

$roles = Role::orderBy('name', 'asc')->lists('name', 'id');
$user = User::find($id);
return View::make('users.edit')->with('user', $user)->with('roles', $roles);

Then in your view:

{{ Form::select('roles', $roles, $user->roles->lists('id'), array('class' => 'select', 'multiple' => true)) }} 

And after that, you can use sync() in the update() function to save the roles:

// ...
$user->save();

$user->roles()->sync(Input::get('roles', []));
// ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM