Different user types table laravel

1.1k Views Asked by At

i have a problem on my project on my user table im using an usertype it has a Student Teacher Admin and i want them to have a separate table with out using an eloquent just a simple query/code or an simple tutorial to help my problem..

controller

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

teacher_table

 @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

**i want to view the Student usertype i don't know please help me **

1

There are 1 best solutions below

2
MaartenDev On

Adding an additional model makes it possible to assign a type/role to a user. Start by creating the Role model:

php artisan make:model Role -m

and then editing the following migration: database/migrations/*_*_creates_roles_table.php to have the following up method:

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');
    });
}

The Role model can now be associated with a User by adding a relation to the user model:

app/User.php

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function role(){
        return $this->hasOne(Role::class);
    }
}

after migrating the database(php artisan migrate) the following code can be used:

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