bertmi01
2 months ago
352
8
Laravel

Write a migration where a column checks multiple columns for a unique value?

Posted 2 months ago by bertmi01

So on my Groups table, I have the columns "student_code" and "chaperone_code". My app generates a random string which I store in these columns. The string is then used in a registration route to set the registering user to the role of student or chaperone.

I want to make sure that any code made is unique across both columns. Is there a way to set that up in the Schema for the migration? I can't figure it out from the docs or looking around elsewhere.

Of course, I could solve this in other ways, such as validation on the Controller and Seeder/Factory or making a pivot table for codes, but I was hoping there was some Laravel magic to use here.

Thanks!

Please sign in or create an account to participate in this conversation.