我創建了一個名為“用戶”的表,在該表中用戶可以擁有三個角色之一。我正在嘗試創建三個單獨的表,以根據他們的角色將不同用戶分組到一個頁面或一個單獨的頁面中。目前,我將它們顯示在一張桌子上。用戶 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 創建表?;蛘吣憧梢允褂?code>belongTorelashionship,按角色獲取用戶,如你所愿。$角色->用戶

呼喚遠方
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 關注
- 104 瀏覽
添加回答
舉報
0/150
提交
取消