user5600983
user5600983

Reputation:

Duplicate Record Issue

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

Answers (2)

Mihai Matei
Mihai Matei

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

user5600983
user5600983

Reputation:

$v = Validator::make($request->all(), [
            'Category' => 'required|max:100|min:5|unique:tblCategory,Category,'
                         . $request->input('CategoryID'). ",CategoryID"
        ]);

Upvotes: 1

Related Questions