laravel-validator icon indicating copy to clipboard operation
laravel-validator copied to clipboard

How to Unique Rule To Ignore A Given ID?

Open copkg opened this issue 8 years ago • 8 comments

hi,I use laravel validator Run into a problem ValidatorInterface::RULE_UPDATE => [ 'nickname' => 'required|unique:table,columnName,id', ] but How to Unique Rule To Ignore table Given id?

copkg avatar Feb 12 '17 06:02 copkg

same question

blaysku avatar Feb 20 '17 05:02 blaysku

Any solution? I have the same question.

vimrookie avatar Mar 15 '17 18:03 vimrookie

try this $this->validator->with($data)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE); In your rules

'username' => 'required|unique:users'

copkg avatar Mar 16 '17 00:03 copkg

It works. But when I try to add another unique rule, not validate the new. Example:

    ValidatorInterface::RULE_UPDATE => [
        'cedula' => 'required|unique:trabajadores',
        'correo' => 'required|unique:trabajadores',
    ],

     $this->validator->with($data)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE);

This ignores for the cedula field but not for the mail field. Thanks @meihuasannong

vimrookie avatar Mar 16 '17 13:03 vimrookie

It worked this way for me:

protected $rules = [
    'name'  => 'required|max:255|unique:departments,name,id'
];

public function update(array $data, $id){
    $this->validator->with($data)->setId($id)->passesOrFail();
    return $this->repository->update($data, $id);
}

heavyrick avatar Mar 30 '17 18:03 heavyrick

Any update on this? Using ->setId($id) doesn't work as $this->id is always set to null for some reason.

kylekz avatar May 14 '17 21:05 kylekz

setId doesn't work if your primary key of the table is not id

garhbod avatar Jul 19 '18 02:07 garhbod

try this $this->validator->with($data)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE); In your rules

'username' => 'required|unique:users'

3 days trying to make it work and it was simple as that... thanks a lot

rafaelcirq avatar Oct 16 '19 20:10 rafaelcirq