How to make a field optional using the validation rule in Laravel

Overview

In this shot, you will learn how to create optional and validated fields submitted from your form. This rule is one of the Laravel validation rules that accepts empty fields and still validates them.

Note: If these fields are NOT NULLABLE on your database, this will throw errors. Therefore, the fields have to be NULLABLE in the database.

Usage

$request->validate([
'publish_at'=>'nullable|date',
]);

The above code uses the nullable Laravel rule.

The code passes validation even though the publish_at field is empty because of the nullable rule.

If the field is filled, it ensures that the field is a date value because it also has the date validation rule.

Free Resources