Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Nullify Laravel Package

michael-rubel/nullify

Nullify is a Laravel package that converts empty values to null, helping keep request and model data consistent and making validation and database storage more predictable.

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package with Composer:

composer require michael-rubel/nullify

Laravel package discovery should register the service provider automatically. There is no required configuration for the basic helper; start by reading the package README and the small src/ directory so you know exactly what it considers “empty.”

The first use case is normalizing optional, nested input before validation or persistence:

$payload = nullify([
    'name' => '',
    'nickname' => '   ',
    'tags' => [],
    'meta' => ['notes' => ''],
    'active' => false,
    'count' => 0,
]);

// Typically:
// ['name' => null, 'nickname' => null, 'tags' => null,
//  'meta' => ['notes' => null], 'active' => false, 'count' => 0]

Use it where Laravel’s ConvertEmptyStringsToNull middleware does not reach: nested arrays, JSON payloads, console commands, jobs, imports, and service-layer input.

Implementation Patterns

Normalize in a Form Request before validation

prepareForValidation() is the natural boundary for request normalization:

protected function prepareForValidation(): void
{
    $this->merge(
        nullify($this->except(['password', 'password_confirmation'])) ?? []
    );
}

Then define rules with nullable:

'nickname' => ['nullable', 'string', 'max:255'],
'tags' => ['nullable', 'array'],

Normalize API payloads outside the HTTP middleware

For JSON endpoints or third-party webhooks, normalize the decoded payload before validation:

$data = nullify($request->all()) ?? [];

$validated = Validator::make($data, [
    'email' => ['required', 'email'],
    'company.name' => ['nullable', 'string'],
    'company.vat' => ['nullable', 'string'],
])->validate();

Normalize at DTO / action boundaries

Keep normalization at the edge of your application, not scattered through domain logic:

final class CreateCustomerData
{
    public function __construct(
        public readonly string $email,
        public readonly ?string $companyName,
        public readonly ?array $tags,
    ) {}

    public static function fromArray(array $input): self
    {
        $input = nullify($input) ?? [];

        return new self(
            email: $input['email'],
            companyName: $input['company_name'] ?? null,
            tags: $input['tags'] ?? null,
        );
    }
}

Use mutators for model-level normalization

If a model should always store blanks as NULL, wrap the attribute setter:

use Illuminate\Database\Eloquent\Casts\Attribute;

protected function nickname(): Attribute
{
    return Attribute::make(
        set: fn ($value) => nullify($value),
    );
}

Use it for “empty means no filter” query logic

$status = nullify($request->input('status'));

$query->when(
    $status === null,
    fn ($q) => $q->whereNull('status'),
    fn ($q) => $q->where('status', $status),
);

Keep Laravel’s middleware for top-level request strings

Do not replace ConvertEmptyStringsToNull. Let Laravel handle ordinary form fields and use Nullify for recursion, arrays, collections, imports, and non-HTTP code.

Gotchas and Tips

  • Check the definition of “empty.” Laravel’s blank() treats 0, '0', and false as non-empty; PHP’s empty() does not. Confirm which behavior the package uses before sending numeric or boolean fields through it.
  • Arrays can become null. If a downstream type hint is array rather than ?array, normalize earlier or wrap with ?? []. This is especially important for DTO constructors and service methods.
  • Database columns must be nullable. Nullifying an empty string is fine in PHP but will fail if the column is NOT NULL.
  • Validation order matters. Normalize before validation, not after. If you nullify required fields after validation, downstream code receives values that never passed the rules.
  • Do not normalize secrets blindly. Avoid passing passwords, tokens, or confirmation fields through broad nullify($request->all()) calls unless you explicitly want that behavior.
  • Files and objects need care. If $request->all() contains uploaded files or domain objects, normalize only the scalar/array fields you intend to touch.
  • Nested behavior is the main reason to use this package. Verify whether normalization is recursive; if it is not, map nested structures yourself or wrap the helper in a project-specific normalizer.
  • Config caching applies. If you publish or modify any package config, run php artisan config:cache in deployment environments.
  • Debug with simple dumps first. Because the package is small, dd(nullify($value)) on representative inputs is usually faster than reasoning about edge cases.
  • Treat it as a small utility, not infrastructure. With a small user base and a single recent release, read the source, pin the version, and wrap the helper in your own application function if you need stable project-specific “empty” semantics.
  • Collection semantics can surprise you. Converting an empty collection to null is different from passing [] to sync(), JSON columns, or APIs that distinguish between “missing” and “empty.” Normalize deliberately at those boundaries.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
skyraptor/chatter
edlugz/sasapay
abbasudo/artisan-route
mirvan/ippanel
ignibyte/aic-framework
glavweb/data-schema-bundle
glavweb/datagrid-bundle
tacticmedia/scim-server
uni-method/frame
neika-ai/sdk
adachsoft/php-code-style
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
adachsoft/api-version-bundle
cgv/localized-resource