Support the ongoing development of Laravel.io →
Authentication Database Validation

How do I perform a Laravel unique validation on multiple columns for unique relations.

One user can add new games but I want it so that one user cannot add new identical games

For example, I don't want to allow this: - table games - id name user_id - 1 Descent 1 - 1 Descent 1

But I want to force the users this:

  • table games
  • id name user_id
  • 1 Descent 1
  • 1 Descent 2

How do I do this in laravel? I apologise for the messy formatting, I can't seem to get it to work

Last updated 2 years ago.
0

Try the unique rule with with additional where clause

'name' => 'unique:games,name,NULL,id,user_id,'.$user->id

http://laravel.com/docs/5.1/validation#rule-unique

Last updated 9 years ago.

dhruvinprajapati liked this reply

1

Thank you so much, it works perfectly!

0

If using auth:

public function rules() {
	return [
		'email' => 'required|email|unique:users,email,NULL,id,deleted_at,NULL,department_id,' . auth()->user()->department_id,
	];
}
Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.