mardi 26 mai 2020

Laravel unique validation Ignore Rule for update

I have a name field on my Client model which must be unique. For the store method I have the following rules:

array (
  'token' => 'string|max:250',
  'directory' => 'max:250',
  'name' => 'required|string|max:250|unique:clients',
)  

For the update method, I have amended this ruleset to ignore the current ID to avoid a duplicate being identified: $rules['name'] = $rules['name'] . ',id,' . $id;

This produces the following ruleset (for record ID 105):

array (
  'token' => 'string|max:250',
  'directory' => 'max:250',
  'name' => 'required|string|max:250|unique:clients,id,105',
)  

When the update method is executed, the The name has already been taken. response is returned during the validation.

I realise this is a well-discussed topic, but believe I am conforming to the correct approach. Can someone point out the error in my logic, please?

Note: This is running on Laravel 5.8.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire