douzhi2012 2015-05-12 10:36
浏览 17
已采纳

Laravel选择用户角色和功能并列出它们

I want to ge the role per user and display them inside an table.

What I want as output is something like this:

output

So the table head Functie needs to be the function name of my database table column name inside table roles.

My controller:

public function index()
    {

        $select_all_users = DB::table('users')->orderBy('username', 'DESC')->get();

        foreach ($select_all_users as $users) {

            //make an array object of the news
            $users = array();

            $select_user_role = DB::table('users')
            ->join('roles', 'users.role_id', '=', 'roles.id')
            ->select('roles.id', 'roles.name', 'roles.description', 'roles.color')
            ->where('username', '=', Auth::user()->username)
            ->first();
        }



        return View::make('admin.users.index')->with('select_user', $select_all_users);
    }

I know that the $select_user_role only will take the value of the users session...

So now I want them listed per user.

My view:

<tbody>

                            @foreach($select_user as $user)

                            <td>{{ ucfirst($user->username) }}</td>

                            <td>{{ ucfirst($user->last_name) }}</td>

                            <td>{{ $user->email }}</td>

                            <td>Here needs to come the role name</td>

                            <td>{{ $user->status }}</td>

                            <td>{{ strftime('%A %d %B %Y', $user->registered_at) }}</td>

                            <td>{{ strftime('%A %d %B %Y', $user->last_login) }}</td>

                            <td><a href="{{ URL::to('admin/user/' . $user->id . '/edit') }}" class="btn btn-sm yellow">Bewerk <i class="fa fa-edit"></i></a></td>

                            <td>
                            {{ Form::open(['method' => 'delete',  'route' => ['admin.user.destroy', $user->id]]) }}
                                {{ Form::button('Verwijderen <i class="fa fa-user-times"></i>', array('type' => 'submit', 'class' => 'btn btn-sm red')) }}
                            {{ Form::close() }}

                            </td>

                            </tr>
                            @endforeach

                            </tbody>

展开全部

  • 写回答

1条回答 默认 最新

  • dongxie548548 2015-05-12 11:04
    关注

    First: add a relation to your user model:

    class User extends Model {
    
        // ...
    
        public function role()
        {
            return $this->belongsTo('App\Role'); // apply your namespace accordingly
        }
    }
    

    Then, within your controller, use eager loading to get all the users with their roles:

    public function index()
    {
        $select_all_users = User::with('role')->get();
    
        return View::make('admin.users.index')->with('select_user', $select_all_users);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部