猿问

根据用户角色创建单独的表

我创建了一个名为“用户”的表,在该表中用户可以拥有三个角色之一。我正在尝试创建三个单独的表,以根据他们的角色将不同用户分组到一个页面或一个单独的页面中。目前,我将它们显示在一张桌子上。


用户 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>

我很感激任何关于我如何去做这件事的帮助。

慕少森
浏览 84回答 2
2回答

慕哥6287543

在控制器中,您需要按用户分组role_name,然后在 index.blade.php 中使用 foreach 创建表。或者你可以使用belongTorelashionship,按角色获取用户,如你所愿。$角色->用户

呼唤远方

您可以尝试制作三个这样的表[数据库]users&nbsp; &nbsp; id - integer&nbsp; &nbsp; name - stringroles&nbsp; &nbsp; id - integer&nbsp; &nbsp; name - stringrole_user&nbsp; &nbsp; user_id - integer&nbsp; &nbsp; role_id - integer[模型]<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class User extends Model{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* The roles that belong to the user.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function roles()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsToMany('App\Role');&nbsp; &nbsp; }}您可以在 [CONTROLLER]中附加许多角色$user = App\User::find(1);$user->roles()->attach($roleId);``
随时随地看视频慕课网APP
我要回答