Laravel Update Model With Unique Validation Rule For Attribute

Laravel: Validation unique on update

One simple solution.

In your Model

protected $rules = [
'email_address' => 'sometimes|required|email|unique:users',
..
];

In your Controller, action:update

...
$rules = User::$rules;
$rules['email_address'] = $rules['email_address'] . ',id,' . $id;
$validationCertificate = Validator::make($input, $rules);

Laravel update model with unique validation rule for attribute

Append the id of the instance currently being updated to the validator.

  1. Pass the id of your instance to ignore the unique validator.

  2. In the validator, use a parameter to detect if you are updating or creating the resource.

If updating, force the unique rule to ignore a given id:

//rules
'email' => 'unique:users,email_address,' . $userId,

If creating, proceed as usual:

//rules
'email' => 'unique:users,email_address',

Laravel 5.4 Validation Request , How to handle unique validation on update?

Please try this

public function rules()
{

$id = $this->request->get('id') ? ',' . $this->request->get('id') : '';

return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$id,
];
}

Laravel validation throws error on updating same unique value

You have to use following code

$this->validate($request, [
'name' => ['required', Rule::unique('myTableName')->ignore($yourVariable->id)],
]);

Add header section below code

use Illuminate\Validation\Rule;

More Details here

Laravel unique validation, only if value is different & without id

You could do the following which uses a where clause to say we want to check the question is not already in the table.

$validator = Validator::make(['question' => $request->question], [
'question' => [
'required',
'string',
Rule::unique('faq_questions')->where('question', '!=', $request->question),
'max:30'
]
]);

You can change the field it checks against to be whatever you want, hopefully the above gives you the general idea.

Then you can check if validation was successful or unsuccessful:

if ($validator->fails()) {
// error
}

Laravel php: unique update validation while editing how to ignore it in updating method

The issue is that you are using your VendorsRequest request handler for both create and update.

The simplest way to solve this is to write a VendorsUpdateRequest handler and do not include the unique options.

Another option would be to move the validation into your controller check which method you are using create or update and return the rules you want for that option.

Laravel validation rule to enforce unique combinations?

If you want to do this in a validator, use a custom validation rule:

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

$validator = Validator::make($request->all(), [
// Change column_b here to the name of your input
'column_b' => function ($attribute, $value, $fail) use ($request) {
$columnB = $value;
// Change column_a here to the name of your input
$columnA = $request->input('column_a');

$records = DB::table('YOUR_TABLE')
->select('*')
// Change column_a here to the name of column A in your database
->where('column_a', $columnA)
// Change column_b here to the name of column B in your database
->where('column_b', $columnB)
->count();

if($records > 0) {
$fail("not allowed because value $columnB in Column B has already been set for value $columnA in column A");
}
},
]);


Related Topics



Leave a reply



Submit