Reputation: 1952
I have a types
table with these columns :
I would like to make the name
field unique. That is to say that if I add the type "Big", I could'nt add the type "Big" again.
I tried this :
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|min:2|unique:types,name'
]);
...
}
If I do that, it works. But if another team adds a type that already exists in database, it tells me that the type already exists.
What I would like to do is make the type unique only for everyone data who has the same team_id
It's possible ?
Thank you very much !
Upvotes: 2
Views: 66
Reputation: 59
You can use like this. https://laravel.com/docs/5.6/validation#using-closures
$this->validate($request, [
`name` => [
`required`,
`min:2`,
function($attribute, $value, $fail) {
/* your query here for check name unique or not */
return $fail($attribute.` is invalid.`);
},
]
]);
Or Use Like This.
Rule::unique('types')->ignore($user->id),
Hope This Helps. :)
Upvotes: 0
Reputation: 403
why not use if condition. if name not unique, check if team_id unique or not
Upvotes: 0
Reputation: 81
Try the following:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|min:2|unique:name'.$request->team_id
]);
...
}
Upvotes: 1
Reputation: 157
primary key can't be repeated so you can't check that two team would have same team_id.
Upvotes: 0