Search code examples
laravellaravel-validation

can be nullable and must be unique validation in laravel


$this->validate($request, [
    'id_num' => 'required | unique:users', //this should be nullable not required with unique.
]);

Is it possible to nullable + unique at a time? then what will be the procedures?


Solution

  • Simply remove the required from the validator.

    Is it possible to nullable + unique at a time?

    Yes

    then what will be the procedures?

    You don't need a procedure, declare the field as unique in the table migration.

    $table->unique('id_num')->nullable();
    

    then your validator must look like:

    $this->validate($request, [
        'id_num' => 'unique:users', //this is nullable since it is not required, but it checks for unique
    ]);