我创建了一个名为“用户”的表,在该表中用户可以拥有三个角色之一。我正在尝试创建三个单独的表,以根据他们的角色将不同用户分组到一个页面或一个单独的页面中。目前,我将它们显示在一张桌子上。用户 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"> <div class="card-header">Manage 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>我很感激任何关于我如何去做这件事的帮助。
2 回答

慕哥6287543
TA贡献1831条经验 获得超10个赞
在控制器中,您需要按用户分组role_name
,然后在 index.blade.php 中使用 foreach 创建表。或者你可以使用belongTo
relashionship,按角色获取用户,如你所愿。$角色->用户

呼唤远方
TA贡献1856条经验 获得超11个赞
您可以尝试制作三个这样的表
[数据库]
users
id - integer
name - string
roles
id - integer
name - string
role_user
user_id - integer
role_id - integer
[模型]
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
您可以在 [CONTROLLER]中附加许多角色
$user = App\User::find(1);
$user->roles()->attach($roleId);
``
- 2 回答
- 0 关注
- 83 浏览
添加回答
举报
0/150
提交
取消