Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Snapey's avatar

@royduin bear in mind that this is only a solution where it is the user themselves that is updating their own record.

Its no use at all if you are trying to update another user (say, as an admin) in which case its better to fall back to using the id from the route, assuming restful routes. This works for the user's own record as well as others.

Note that if using route binding, the parameter in the url might be other than id, for instance

Route::get('/members/{member}/update', 'MemberController@update');

then, in the form request, $this->member is what you need for the unique rule ignore attribute

afraz's avatar

In Request class for PUT or PATCH method you can also do this

 use Illuminate\Validation\Rule;


 'email' => Rule::unique('users')->ignore($this->id)

Because id is available in object or Request class.

sghimire's avatar

Hi there,

In the below example the report is an array I want to write the rules to validate array Item i.e. report.id - is this possible to do it? if not please can you suggest me on this.

$rules = [ 'title' =>'required|string|max:255', 'slug' =>"required|alpha_dash|min:5|max:255|unique:posts,slug", 'image' =>'sometimes|image', 'category_id' =>'required|integer', 'report' =>'required' ];

Thanking in advanced!

sghimire's avatar

protected $rules = [

    'title' =>'required|string|max:255',
    'slug' =>"required|alpha_dash|min:5|max:255|unique:posts,slug",
    'image' =>'sometimes|image', 'category_id' =>'required|integer',
    'reports.report_type_id' =>'required|integer'
];

The above request is validating but the reports(json payload) can have multiple entities as below:

{ "data":{ "generated_by": "foobar",

  "reports":[  
     {  
        "report":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus",
        "report_type_id":1,
        "test_id": 388594
     },
      {  
        "report":"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo",
        "report_type_id":1,
        "test_id": 388595
     },
     
      {  
        "report":"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo",
        "report_type_id":2,
        "test_id": 388596
     }
    
  ],
  

} }

sghimire's avatar

protected $rules = [

    'title' =>'required|string|max:255',
    'slug' =>"required|alpha_dash|min:5|max:255|unique:posts,slug",
    'image' =>'sometimes|image', 'category_id' =>'required|integer',
    'reports.*.report_type_id' =>'required|integer',
];

I have tried as above but it did not work for me too.

bashy's avatar

@sghimire You will need to do two rules for it;

protected $rules = [
    'title' => 'required|string|max:255',
    'slug' => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
    'image' => 'sometimes|image',
    'category_id' => 'required|integer',
    'reports' => 'required',
    'reports.*.report_type_id' => 'required|integer'
];
sghimire's avatar

Thanks for your reply. However I am still getting the below error message all the time. That means validation is triggering all the times. Please can you help on this.

{
    "message": {
        "reports": [
            "The reports field is required."
        ]
    }
}

bashy's avatar

@sghimire Then put the rule of sometimes? You didn't say you wanted it to be an optional field?

sghimire's avatar

Sorry for the confusion .. reports is a array and it contains multiple values.

protected $rules = [

    'title'                                 =>'required|string|max:255',
    'slug'                                  =>"required|alpha_dash|min:5|max:255|unique:posts,slug",
    'image'                                 =>'sometimes|image', 
    'category_id'                       =>'required|integer',
    'reports'                               => 'sometimes',
    'reports.*.report_type_id'  => 'required|integer',

];

I have implemented as you suggested , however it is not working the validation for me, if I do not include the "report_type_id" as below:

"reports":[  
     {  
        "report":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus",        
        "test_id": 388594
     },
      {  
        "report":"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo",
        "report_type_id":1,
        "test_id": 388595
     },
     
      {  
        "report":"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo",
        "report_type_id":2,
        "test_id": 388596
     }
    
  ],

It is throwing database(MYSQL) error but it is not triggering the validation at all.

{
"message": "Undefined index: report_type_id",
    "status_code": 500,
}

Thank you for your great help!

sghimire's avatar

Hi bashy,

Thanks for helping on my above query, however I am still having the problem with this validation. Please can you help.

Many thanks for your time.

roark's avatar

@CALLAM - This is great thank you, saved me a headache. Might also be worth adding sometimes validation rules in the PATCH block as the user might only submit certain values?

case 'PATCH':
        {
            return [
                'user.name.first' => 'sometimes|required',
                'user.name.last'  => 'sometimes|required',
                'user.email'      => 'sometimes|required|email|unique:users,email,'.$user->id,
                'user.password'   => 'sometimes|required|confirmed',
            ];
        }
malcolmg's avatar

Arrrrgh! And don't be like me an add a SPACE after table name, column name: 'user.email' => 'sometimes|required|email|unique:users,email,'.$user->id,

I spent an hour wondering why the query couldn't find the column name! Couldn't see on on my screen that it was not stripping out the whitespace.

Time for coffee. Hope this helps someone

minaFaragAmin's avatar

for me i just dumped the route like dd( $this->route() ) then checked the route name and it was customer , later i just used

$this->route('customer')

otavio_araujo's avatar

You just have to:

public function rules() { return [ 'email' => 'required|email|unique:users,email,:id', // others ]; }

hassam's avatar

How to manage unique rule in Laravel Nova

Nova version v4.25.1 (Silver Surfer)

For Laravel Nova use ->creationRules() and ->updateRules() methods.

// app\Nova\Expense.php

Text::make('Type')
    ->sortable()
    ->rules('required', 'string')
    ->creationRules('unique:expenses,type')
    ->updateRules('unique:expenses,type,{{resourceId}}'),
Previous

Please or to participate in this conversation.