I
I
Ilya Parshakov2018-08-09 12:40:41
Laravel
Ilya Parshakov, 2018-08-09 12:40:41

How to validate a pair of values ​​from several tables for uniqueness?

Hello.
For example, there is a users table and a groups table .
users : id, name, email.
groups : id, name, slug.
How to set up validation to check and prevent user registration with the same email , within a group (name from groups).
That is, users can register with the same email multiple times, but the users.email and groups.name pair must be unique.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Faim, 2018-08-09
@Faim

Most likely, you should have a group_id column in your users table to link a user to a group.
In this case, the rule is:

'email' => Rule::unique('users')->where('group_id',  $request->input('group_id'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question