harryg
4 years ago

Validating a composite unique field

Posted 4 years ago by harryg

So I have a Term model (and corresponding migration).

A Term has fields taxonomy and term. It's used to hold things like tags and categories.

So, say for a "movies" category you would create it with:

Term::create(['taxonomy' => 'category', 'term' => 'movies']);

You could still have a "movies" tag:

Term::create(['taxonomy' => 'tag', 'term' => 'movies']);

But once a "movies" category has been created, you shouldn't be able to create another. I.e. there is a composite unique field between terms.term and terms.taxonomy.

Is there a way to define such a validation rule for this?

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