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

mhmmdva's avatar

3 Role status in Migration

i created 3 roles in migration and the data cannot be inserted

users

  public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id(); // primary key

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

            // identifier       
            $table->enum('role', ['Admin', 'Lecturer', 'Reviewer']);
           
            // system
            $table->timestamp('email_verified_at')->nullable();
            $table->timestamps();           
            $table->rememberToken();    
            
        });

user model

class User extends Authenticatable
{
 use HasApiTokens, HasFactory, Notifiable;

    const LECTURER= 'LECTURER';
    const ADMIN = 'Admin';
    const REVIEWER = 'Reviewer';

 protected $guarded = ['id'];
}

LecturerRequest

class LecturerRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $validation = [
            'nidn' => ['required', 'numeric', 'min:4'],
            'fullname' => ['required', 'string', 'max:255'],
            'email' => ['required', 'email'],
            'password' => [
                'nullable', 'confirmed', 'string',
                Password::min(8)
                    ->letters()
                    ->mixedCase()
                    ->numbers()
                    ->symbols()
                    ->uncompromised()
                // ex : P@ssw0rd
            ],
           
            'role' => ['required', 'string',  User::DOSEN]
        ];

        return $validation;
    }
}

LecturerController

 public function store(LecturerRequest $request, User $user)
    {
        $data = $request->validated();  

        $data = $request->role['Lecturer'];

        if ($request->filled('password')) {
            $data['password'] = bcrypt($data['password']);
        } else {
            $data['password'] = $user->password;
        }
 
        User::query()->create($data); 
        
        
        return redirect()->route('lpp.lecturer.index')->with('success', 'Selamat, data telah berhasil anda tambahkan!');
    }
0 likes
4 replies
mhmmdva's avatar

typo in user model

class User extends Authenticatable
{
 use HasApiTokens, HasFactory, Notifiable;

    const LECTURER= 'Lecturer';
    const ADMIN = 'Admin';
    const REVIEWER = 'Reviewer';

 protected $guarded = ['id'];
}
Snapey's avatar
Snapey
Best Answer
Level 122

you array of variables to write to model needs to be array of key => value

mhmmdva's avatar

@Snapey so that means I have to make it like this :

const ROLE = ['Admin', 'Lecturer', 'Reviewer'];

mhmmdva's avatar

problem is solved : i deleted the role in validation and i called the const variable in user model

LecturerController

 public function store(LecturerRequest $request, User $user)
    {
        $data = $request->validated();
        
        $data['role'] = User::REVIEWER;

        if ($request->filled('password')) {
            $data['password'] = bcrypt($data['password']);
        } else {
            $data['password'] = $user->password;
        }
 
        User::create($data); 
                    
        return redirect()->route('lpp.lecturer.index')->with('success', 'Selamat, data telah berhasil anda tambahkan!');
    }

LecturerRequest

 public function rules()
    {
        $validation = [
            'nidn' => ['required', 'numeric', 'min:4'],
            'fullname' => ['required', 'string', 'max:255'],
            'email' => ['required', 'email'],
            'password' => [
                'nullable', 'confirmed', 'string',
                Password::min(8)
                    ->letters()
                    ->mixedCase()
                    ->numbers()
                    ->symbols()
                    ->uncompromised()
                // ex : P@ssw0rd
            ],
 
            // 'role' => ['nullable', 'string',  Rule::in(['Admin', 'Dosen', 'Reviewer'])],
        ];

        return $validation;
    }

Please or to participate in this conversation.