Reputation:
I am trying to update the record. But due to some reasons I am always getting duplicate record validation.
Here is my code
public function update(Request $request)
{
$v = Validator::make($request->all(), [
'Category' => 'required|unique:tblcategory|max:100|min:5'
]);
if ($v->fails()) {
return redirect('Category/'.$request->input('CategoryID'))
->withErrors($v)
->withInput();
}
}
Am I missing something ?
Upvotes: 2
Views: 86
Reputation: 24276
On update you must add the entity ID to avoid finding the same entity when validating the uniqueness:
$v = Validator::make($request->all(), [
'Category' => 'required|max:100|min:5|unique:tblcategory,Category,'.$request->input('CategoryID').',CategoryID';
]);
It is also documented on laravel docs
Upvotes: 0
Reputation:
$v = Validator::make($request->all(), [
'Category' => 'required|max:100|min:5|unique:tblCategory,Category,'
. $request->input('CategoryID'). ",CategoryID"
]);
Upvotes: 1