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
Laravel Nullable Fields

Laravel Nullable Fields Laravel Package

dyrynda/laravel-nullable-fields

Keeps nullable database columns genuinely null. Add the NullableFields trait to an Eloquent model and list your nullable fields; empty strings are converted to NULL on save, keeping your Laravel data consistent across queries, filters, and relationships.

View on GitHub
Deep Wiki
Context7

Handles saving empty fields as null for Eloquent models

Frequently asked questions about Laravel Nullable Fields
How do I install dyrynda/laravel-nullable-fields in a Laravel app?
Install it with Composer using composer require dyrynda/laravel-nullable-fields. There is no service provider, configuration file, middleware, or migration requirement. You opt in per Eloquent model by adding the trait and declaring your nullable attributes.
How do I use the NullableFields trait on an Eloquent model?
Add use NullableFields to the model and define a protected $nullable array listing the attributes that should be normalized. The trait hooks into the model saving event and converts empty strings to NULL before persistence. Apply it model by model so risk stays contained to opted-in models.
Which Laravel and PHP versions are supported by this package?
The package is a thin Eloquent model-event trait with no framework-specific constraints beyond standard saving behaviour, making it suitable for Laravel 8 and newer applications. You should validate its Composer constraints against your exact Laravel major version and PHP runtime in staging. There are no database-specific requirements, but MySQL, Postgres, and SQL Server empty-string and NULL semantics should be checked for your driver.
Does dyrynda/laravel-nullable-fields work with mass updates like Model::where()->update()?
No, the trait only runs through the Eloquent saving model event. Mass update queries such as Model::where(...)->update() bypass model events entirely, so empty strings will not be converted to NULL on those paths. You need to handle bulk normalization explicitly or treat it as a separate data migration concern.
Will converting empty strings to NULL conflict with Laravel validation rules?
It can conflict with rules such as required, nullable, and unique because validation may treat an empty string differently from NULL. Unique constraints can also behave differently when NULL values are involved. Coordinate validation-layer changes so nullable rules match the new persistence behaviour before rolling the trait out broadly.
Should I depend on the package or vendor the trait into my codebase?
Given the package has roughly 102 stars and zero dependents, it is an unmaintained-by-community micro-package that your team may effectively need to own. Vendoring the approximately 40-line trait is a defensible default because it removes supply-chain and abandonment risk at negligible cost. The integration steps are identical minus the Composer entry.
Does the trait handle custom casts, accessors, and mutators correctly?
This needs explicit verification in your application rather than assumption. The trait sets attributes to NULL before persistence, which can interact with casts such as AsArrayObject or custom casts and with accessors or mutators that assume string types. Test the specific models and cast combinations you plan to opt in.
How should I test that empty fields are saved as NULL?
Write feature tests that create or update records through the Eloquent save path and assert the database column is NULL rather than an empty string. Compare persisted rows before and after enabling the trait in a staging environment. Also explicitly test forceFill, create, and any insert or upsert paths because some of those bypass model events.
Is this package safe to use in production?
The runtime impact is minimal because the trait has no configuration, background processes, or external service dependencies. The main production concern is governance: with no downstream dependents to surface regressions, assume first-line support is internal and assign a named owner. Budget for periodic compatibility review on Laravel upgrades and keep rollback simple by removing the trait from a model.
Does it affect insert(), upsert(), or forceFill paths?
forceFill only sets attributes in memory, so a subsequent save still triggers the trait's saving event and normalization. However, insert() and upsert() bypass Eloquent model events, so they will not apply the empty-string-to-NULL conversion. Verify each write path in your application, especially bulk imports and raw query builder usage.
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.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections