douzhi2012 2015-05-12 18: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 19: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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害