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

kevariable's avatar

prevent register if 2 relation have same value

hello everyone!, i have 2 relation which is Student and Teacher and my primary login system as parent is User

so i have the structure model output is

as teacher

App\Models\User {#4273
         id: "35bd4d43-302e-4caf-ac4a-4317a6440e2f",
         first_name: "Zachery",
         last_name: "Heller",
         email: "[email protected]",
         email_verified_at: "2020-10-05 08:45:37",
         two_factor_secret: null,
         two_factor_recovery_codes: null,
         created_at: "2020-10-05 08:45:37",
         updated_at: "2020-10-05 08:45:37",
         teacher: App\Models\Teacher {#4297
           id: 9,
           user_id: "35bd4d43-302e-4caf-ac4a-4317a6440e2f",
           nik: "1111",
           created_at: "2020-10-05 08:45:37",
           updated_at: "2020-10-05 08:45:37",
         },
       },

and as student

App\Models\User {#4282
         id: "f294b6ba-fa08-4269-8640-678d6f2cd8e7",
         first_name: "Annetta",
         last_name: "Donnelly",
         email: "[email protected]",
         email_verified_at: "2020-10-05 08:45:36",
         two_factor_secret: null,
         two_factor_recovery_codes: null,
         created_at: "2020-10-05 08:45:36",
         updated_at: "2020-10-05 08:45:36",
         student: App\Models\Student {#4293
           id: 5,
           user_id: "f294b6ba-fa08-4269-8640-678d6f2cd8e7",
           nim: "1111",
           created_at: "2020-10-05 08:45:36",
           updated_at: "2020-10-05 08:45:36",
         },
       },

so i want to prevent nim and nik is unique

0 likes
4 replies
automica's avatar
automica
Best Answer
Level 54

@kevariable can you explain how you register your users?

I assume that the process to register a teacher is different to registering a parent so teacher registration comes first?

your check will be:

if($user->student && $user->teacher && ($user->student->nim === $user->teacher->nik)){
 throw new Exception("A User cannot be a teacher and a student");
}

and will need to go in wherever you are creating your new teachers & students.

1 like
kevariable's avatar

i solve my issue with relation, thanks for answered

kevariable's avatar

i thinks your answer is right sir. it's work to

1 like

Please or to participate in this conversation.