You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按用户角色拆分显示独立的用户数据表?

如何按角色分组显示用户数据表?

我来帮你搞定这个按角色分组展示用户的需求,分两步操作就能实现:


第一步:修改控制器拆分用户数据

首先找到你的用户管理控制器(一般是app/Http/Controllers/Admin/UserController.php),修改index方法,把用户按角色拆分成三个独立的集合,传递到视图中:

public function index()
{
    // 替换这里的角色名称为你实际的角色标识(比如'admin'/'teacher'/'student')
    $adminUsers = User::whereHas('roles', function($query) {
        $query->where('role_name', 'admin');
    })->get();

    $teacherUsers = User::whereHas('roles', function($query) {
        $query->where('role_name', 'teacher');
    })->get();

    $studentUsers = User::whereHas('roles', function($query) {
        $query->where('role_name', 'student');
    })->get();

    return view('admin.users.index', compact('adminUsers', 'teacherUsers', 'studentUsers'));
}

第二步:修改视图渲染分组表格

替换resources/views/admin/users/index.blade.php的内容,把原来的单个表格改成三个独立的卡片表格,每个表格对应一个角色组:

@extends('layouts.app')
@section('content')
<div class="container">
 <div class="row justify-content-center">
 <div class="col-md-10">
 <p>
 <a href="{{ route('admin.users.create') }}"><button type="button" class="btn btn-success">Create User</button></a>
 </p>

 <!-- 管理员用户表格 -->
 <div class="card mb-4">
 <div class="card-header">Manage Admin Users</div>
 <div class="card-body">
 <div class="table-responsive">
 <table class="table">
 <thead>
 <tr>
 <th>ID</th>
 <th>Name</th>
 <th>Course</th>
 <th>Role</th>
 <th></th>
 <th></th>
 <th></th>
 </tr>
 </thead>
 <tbody>
 @foreach ($adminUsers as $user)
 <tr>
 <th scope="row">{{ $user->id }}</th>
 <td>{{ $user->name}}</td>
 <td>{{ implode(', ', $user->courses()->pluck('title')->toArray()) }}</td>
 <td>{{ implode(', ', $user->roles()->pluck('role_name')->toArray()) }}</td>
 <td>
 @can('edit_users')
 <a class="btn btn-xs btn-secondary" href="{{ route('admin.users.show', $user->id) }}"> View </a>
 @endcan
 </td>
 <td>
 @can('edit_users')
 <a class="btn btn-xs btn-primary" href="{{ route('admin.users.edit', $user->id) }}"> Edit </a>
 @endcan
 </td>
 <td>
 @can('delete_users')
 <form action="{{ route('admin.users.destroy', $user->id) }}" method="POST" onsubmit="return confirm('Confirm delete?');" style="display: inline-block;">
 <input type="hidden" name="_method" value="DELETE">
 <input type="hidden" name="_token" value="{{ csrf_token() }}">
 <input type="submit" class="btn btn-xs btn-danger" value="Delete">
 @endcan
 </form>
 </td>
 </tr>
 @endforeach
 </tbody>
 </table>
 </div>
 </div>
 </div>

 <!-- 教师用户表格 -->
 <div class="card mb-4">
 <div class="card-header">Manage Teacher Users</div>
 <div class="card-body">
 <div class="table-responsive">
 <table class="table">
 <thead>
 <tr>
 <th>ID</th>
 <th>Name</th>
 <th>Course</th>
 <th>Role</th>
 <th></th>
 <th></th>
 <th></th>
 </tr>
 </thead>
 <tbody>
 @foreach ($teacherUsers as $user)
 <tr>
 <th scope="row">{{ $user->id }}</th>
 <td>{{ $user->name}}</td>
 <td>{{ implode(', ', $user->courses()->pluck('title')->toArray()) }}</td>
 <td>{{ implode(', ', $user->roles()->pluck('role_name')->toArray()) }}</td>
 <td>
 @can('edit_users')
 <a class="btn btn-xs btn-secondary" href="{{ route('admin.users.show', $user->id) }}"> View </a>
 @endcan
 </td>
 <td>
 @can('edit_users')
 <a class="btn btn-xs btn-primary" href="{{ route('admin.users.edit', $user->id) }}"> Edit </a>
 @endcan
 </td>
 <td>
 @can('delete_users')
 <form action="{{ route('admin.users.destroy', $user->id) }}" method="POST" onsubmit="return confirm('Confirm delete?');" style="display: inline-block;">
 <input type="hidden" name="_method" value="DELETE">
 <input type="hidden" name="_token" value="{{ csrf_token() }}">
 <input type="submit" class="btn btn-xs btn-danger" value="Delete">
 @endcan
 </form>
 </td>
 </tr>
 @endforeach
 </tbody>
 </table>
 </div>
 </div>
 </div>

 <!-- 学生用户表格 -->
 <div class="card">
 <div class="card-header">Manage Student Users</div>
 <div class="card-body">
 <div class="table-responsive">
 <table class="table">
 <thead>
 <tr>
 <th>ID</th>
 <th>Name</th>
 <th>Course</th>
 <th>Role</th>
 <th></th>
 <th></th>
 <th></th>
 </tr>
 </thead>
 <tbody>
 @foreach ($studentUsers as $user)
 <tr>
 <th scope="row">{{ $user->id }}</th>
 <td>{{ $user->name}}</td>
 <td>{{ implode(', ', $user->courses()->pluck('title')->toArray()) }}</td>
 <td>{{ implode(', ', $user->roles()->pluck('role_name')->toArray()) }}</td>
 <td>
 @can('edit_users')
 <a class="btn btn-xs btn-secondary" href="{{ route('admin.users.show', $user->id) }}"> View </a>
 @endcan
 </td>
 <td>
 @can('edit_users')
 <a class="btn btn-xs btn-primary" href="{{ route('admin.users.edit', $user->id) }}"> Edit </a>
 @endcan
 </td>
 <td>
 @can('delete_users')
 <form action="{{ route('admin.users.destroy', $user->id) }}" method="POST" onsubmit="return confirm('Confirm delete?');" style="display: inline-block;">
 <input type="hidden" name="_method" value="DELETE">
 <input type="hidden" name="_token" value="{{ csrf_token() }}">
 <input type="submit" class="btn btn-xs btn-danger" value="Delete">
 @endcan
 </form>
 </td>
 </tr>
 @endforeach
 </tbody>
 </table>
 </div>
 </div>
 </div>
 </div>
 </div>
 </div>
@endsection

额外说明

  1. 记得把控制器里的角色名称替换成你系统中实际的角色标识,比如如果你的角色是'super_admin'、'editor'、'viewer',就对应修改查询条件里的role_name值。
  2. 如果想要把不同角色的用户放到不同页面显示,只需要把每个角色的表格拆分成单独的视图文件,然后在控制器里新增对应的方法(比如showAdmins、showTeachers)和路由即可。
  3. 我顺便修复了你原代码里implode的小问题——原来的写法缺少分隔符,现在改成implode(', ', ...)后,多个课程/角色会用逗号分隔,显示更美观。

内容的提问来源于stack exchange,提问作者Zak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 00:37:37