Php 根据用户角色创建单独的表
我创建了一个名为“users”的表,在这个表中,用户可以有三个角色之一。我正在尝试创建三个单独的表,以便根据用户的角色在一个页面或单独的页面中对不同的用户进行分组。现在我把它们放在一张桌子上 User index.blade.phpPhp 根据用户角色创建单独的表,php,laravel,Php,Laravel,我创建了一个名为“users”的表,在这个表中,用户可以有三个角色之一。我正在尝试创建三个单独的表,以便根据用户的角色在一个页面或单独的页面中对不同的用户进行分组。现在我把它们放在一张桌子上 User index.blade.php @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center">
@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>
@foreach ($users 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
@extends('layouts.app'))
@节(“内容”)
管理用户
身份证件
名称
课程
角色
@foreach($users作为$user)
{{$user->id}
{{$user->name}
{{内爆($user->courses()->pull('title')->toArray())}
{{内爆(',',$user->roles()->pull('role_name')->toArray())}
@can(“编辑用户”)
@恩德坎
@can(“编辑用户”)
@恩德坎
@can('delete_users')
@恩德坎
@endforeach
@端部
我很感激你能帮我做这件事
在控制器中,您需要按
角色\u名称对用户进行分组,然后在index.blade.php中使用foreach创建表。或者你可以使用belongTo
relaship,并按角色获取用户,如你所愿$角色->用户您可以尝试制作这样的三个表
[数据库]
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');
}
}
$user = App\User::find(1);
$user->roles()->attach($roleId);
``
[型号]
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');
}
}
$user = App\User::find(1);
$user->roles()->attach($roleId);
``
要么实现经典的控制中断逻辑;或者使用两个嵌套循环,外部循环遍历所有可能的角色(foreach(['admin','user','loser']作为$role)
),内部循环则仅为当前记录创建输出,前提是其角色与外部循环的值匹配。