Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mhmmdva's avatar

How to create system for 'change role permisson'

so I have 3 roles, namely Admin, Lecturer and Student. I want to use both roles in 1 account for Lecturers and Students. Can I create a role replacement system? How to make this system, give me input and suggestions

users

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id(); // primary key

            // main data
            $table->bigInteger('nidn'); 
            $table->string('fullname');
            $table->string('email')->unique();
            $table->string('password');

            // detail
            $table->enum('faculty', ['FTIK', 'FEIS']);
            $table->string('major')->nullable();
            $table->string('telp')->unique()->nullable();
            $table->date('birthdate')->nullable();
            $table->enum('gender', ['F', 'M'])->nullable();
            $table->text('address')->nullable();
            $table->text('image')->nullable();

            // identifier
            $table->enum('role', ['Admin', 'Lecturer', 'Student'])->default('Lecturer');

            // system
            $table->timestamp('email_verified_at')->nullable();
            $table->timestamps();

            $table->rememberToken();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
0 likes
5 replies
Tray2's avatar

You change the relationship to a many-to-many instead of a one-to-many, then you can add as many roles as you like to each user.

mhmmdva's avatar

@Tray2 what if I creating a function changeRole ?

public function changeRole(RoleRequest $request, User $profile)
    {
        $data = $request->validated();

        $student= User::STUDENT;
        $lecturer = User::LECTURER;
        $data['role'] = $student;
        dd($data);
        if ($role == $lecturer) {


            $profile->update($data);

            return redirect()->route('lpp.index')->with('success', 'congrast');
        }
    }

notes: the code is still not finished because, there is an error in the logic

Tray2's avatar

@mhmmdva No, the user can have two roles at the same time, that means that you need to implement it properly, and changing the user role will give you a headache.

Snapey's avatar

the simplest change is to alter the column on the database to include a "student-lecturer" role then in your code where you need to check permissions for instance "are they a student" you would need to check are they a student or are they a student-lecturer. Or are they lecturer or are they a student+lecturer, rather than just checking just one role, you now have to check to 2 possible values.

Please or to participate in this conversation.