dyrynda / laravel-nullable-fields
This trait allows you to easily flag attributes that should be set as null when being persisted to the database using the Laravel PHP Framework.
Installs: 140 965
Dependents: 0
Suggesters: 0
Security: 0
Stars: 104
Watchers: 4
Forks: 11
Open Issues: 0
Type:utility
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11.0
- illuminate/database: ^10.0|^11.0
- illuminate/events: ^10.0|^11.0
Requires (Dev)
- pestphp/pest: ^2.34
README
Often times, database fields that are not assigned values are defaulted to null
. This is particularly important when creating records with foreign key constraints, where the relationship is not yet established.
public function up() { Schema::create('profile_user', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->nullable()->default(null); $table->foreign('user_id')->references('users')->on('id'); $table->string('twitter_profile')->nullable()->default(null); $table->string('facebook_profile')->nullable()->default(null); $table->string('linkedin_profile')->nullable()->default(null); $table->text('array_casted')->nullable()->default(null); $table->text('array_not_casted')->nullable()->default(null); }); }
More recent versions of MySQL will convert the value to an empty string if the field is not configured to allow null. Be aware that older versions may actually return an error.
Laravel does not currently support automatically setting nullable database fields as null
when the value assigned to a given attribute is empty.
Installation
This trait is installed via Composer. To install, simply add it to your composer.json
file:
$ composer require dyrynda/laravel-nullable-fields
In order to use this trait, import it in your Eloquent model, then set the protected $nullable
property as an array of fields you would like to be saved as null
when empty.
<?php use Illuminate\Database\Eloquent\Model; use Dyrynda\Database\Support\NullableFields; class UserProfile extends Model { use NullableFields; protected $nullable = [ 'facebook_profile', 'twitter_profile', 'linkedin_profile', 'array_casted', 'array_not_casted', ]; protected $casts = [ 'array_casted' => 'array', ]; }
Now, any time you are saving a UserProfile
profile instance, any empty attributes that are set in the $nullable
property will be saved as null
.
<?php $profile = new UserProfile::find(1); $profile->facebook_profile = ' '; // Empty, saved as null $profile->twitter_profile = 'michaeldyrynda'; $profile->linkedin_profile = ''; // Empty, saved as null $profile->array_casted = []; // Empty, saved as null $profile->array_not_casted = []; // Empty, saved as null $profile->save();
If you want to extend this behaviour to all fields on your model, you may do so by specifying $nullable
as *
:
class UserProfile extends Model { use NullableFields; protected $nullable = '*'; }
More information
Working with nullable fields in Eloquent models - first iteration
Working with nullable fields in Eloquent models - Part Deux - second iteration, covers the details of this package
Support
If you are having general issues with this package, feel free to contact me on Twitter.
If you believe you have found an issue, please report it using the GitHub issue tracker, or better yet, fork the repository and submit a pull request.
If you're using this package, I'd love to hear your thoughts. Thanks!
Treeware
You're free to use this package, but if it makes it to your production environment you are required to buy the world a tree.
It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to plant trees. If you support this package and contribute to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
You can buy trees here
Read more about Treeware at treeware.earth