laravelvalidationrules

Laravel Rule ignore and deleted_at null


I have a validation rule in my update function

Rule currently selects unique from deleted rows.

I have row soft deleted.

Validation Rule should ignore soft delete and check unique and also ignore updating the same row

return [
           'attribute_type_id' => 'required',
           'name' => [
                'required',
                Rule::unique('attributes')->ignore($ignore),
            ],
           'order' => 'integer|nullable'
       ];

This is my update rule

I have added deleted_at rule in create function

return [
           'attribute_type_id' => 'required',
           'name' => 'bail|required|unique:attributes,deleted_at,NULL',
           'order' => 'integer|nullable'
       ];

But not able to do it in update function.

Can somebody help.


Solution

  • You can put a condition in Rule,

    use Illuminate\Validation\Rule;
    ...
    
    'name' => Rule::unique('attributes')->where(function ($query) {
        return $query->whereNull('deleted_at');
    })