不同的用户类型表laravel

不同的用户类型表laravel,laravel,usertype,Laravel,Usertype,我在我的用户表上的项目上有一个问题,我使用的是一个用户类型,它有一个学生教师管理员,我希望他们有一个单独的表,而不使用一个有说服力的简单查询/代码或一个简单的教程来帮助我解决问题 控制器 public function index() { $users = User::find(); return view('teacherpage.teacher_table', compact('users')); } 教师桌 @foreach ($users as $position)

我在我的用户表上的项目上有一个问题,我使用的是一个用户类型,它有一个学生教师管理员,我希望他们有一个单独的表,而不使用一个有说服力的简单查询/代码或一个简单的教程来帮助我解决问题

控制器

public function index()
{
    $users = User::find();
    return view('teacherpage.teacher_table',  compact('users'));
}
教师桌

 @foreach ($users as $position)


                <tbody>
                    <tr>
                        <td>{{ $position->id}}</td>
                        <td>{{ $position->first_name}}</td>
                        <td>{{ $position->last_name}}</td>
                        <td>{{ $position->contact}}</td>
                        <td>{{ $position->department}}</td>


                        <td>{{ $position->usertype}}</td>


                        <td>{{ $position->email}}</td>


                        <th> 
                            <a href="{{action('AdminTableController@edit',['id' =>$position->id])}}" class="btn btn-success">Edit </a>
                    </th>


                            </tr>
                        </tbody>
                        @endforeach
@foreach($users as$position)
{{$position->id}
{{$position->first_name}
{{$position->last_name}
{{$position->contact}
{{$position->department}
{{$position->usertype}
{{$position->email}
@endforeach

**我想查看我不知道的学生用户类型请帮助我**

添加其他模型可以将类型/角色分配给用户。首先创建
角色
模型:

php artisan make:model Role -m
然后编辑以下迁移:
database/migrations/*\u*\ u创建\u角色\u table.php
以具有以下后续方法:

public function up()
{
    Schema::create('roles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
    });

    Schema::table('users', function (Blueprint $table) {
        $table->unsignedBigInteger('role_id')->nullable();

        $table->foreign('role_id')->references('id')->on('roles');
    });
}
角色
模型现在可以通过向用户模型添加关系与用户关联:

app/User.php

迁移数据库(
php artisan migrate
)后,可以使用以下代码:

public function index()
{
    $users = User::with('role')->get();
    return view('teacherpage.teacher_table',  compact('users'));
}

你试过什么?你能添加一些代码吗?我没有,因为我不知道从哪里开始。你的问题框架将“简单”与雄辩/模型形成对比,因此我认为你可能从错误的角度看待这个问题。您可以继续为三张表中的每一张制作单独的模型。你可以看看Laravel的开箱即用的用户模型的特点和界面,以获得框架,将其识别为“用户”模型,并将其应用到你自己的模型中。。但如果你不需要这些铃铛和口哨,这部分是可选的。但就我个人而言,我会使用MaartenDev的多角色方法,而不是多用户表。我对undefined方法Illumb\Database\Eloquent\Builder::all()进行了错误调用。我将其更新为使用
->get()
检查@JohnCliff示例
public function index()
{
    $users = User::with('role')->get();
    return view('teacherpage.teacher_table',  compact('users'));
}