Let's say you have a users table with the column email and email must be unique per user, you can use the following validation rule in Laravel.
[
'email' => 'unique:users,email,' . $user->id,
]
In my case, my users table looks like following
id
email
type
For my use case, the combination of email and type must be unique, which means that email can exist more than one time, and so does type. But the combination of email and type must be unique. Desired result looks like following
id email type
1 A@mail.com 1 // OK! unique
2 A@mail.com 2 // OK! unique
3 B@mail.com 1 // OK! unique
4 B@mail.com 2 // OK! unique
5 A@mail.com 1 // NOT OK! already exists with id = 1.
6 B@mail.com 2 // NOT OK! already exists with id = 4.
How to write a rule to validate that a combination of email and type must be unique in Laravel, PHP, to prevent record with id 5 and 6 from being inserted into database?