← All writing

Choose sometimes and nullable for different PATCH meanings

Laravel 13 / PHP 8.3+Sources checked 2026-09-14

A profile PATCH request may omit a biography to leave it unchanged, or send null to clear it. Treating both cases as identical can erase existing text when a client only intended to update the display name.

Validate the three states

$data = $request->validate([
    'bio' => ['sometimes', 'nullable', 'string', 'max:2000'],
]);
if (array_key_exists('bio', $data)) {
    $profile->bio = $data['bio'];
}

sometimes applies the field's validation when it is present. nullable allows null through the relevant rules. The explicit key check preserves the distinction between absence and an intentional null value.

Account for request middleware

Typical Laravel middleware trims strings and converts empty strings to null. If your API distinguishes an empty string from null, understand that normalization and configure the boundary deliberately. Do not write tests that bypass middleware while assuming they reproduce the real request behavior.

The database column must allow the cleared state if null is accepted. Otherwise validation promises an operation that storage rejects with a server error.

Test omitted bio, null bio, empty input, normal text, and an excessive length. Assert the stored value after each request. Reusing a create-form rule for PATCH without considering omission is a common cause of accidental data loss. Create, full replacement, and partial update are different contracts even when they share a model.

Reference

Official documentation.