PHPackages                             iatstuti/laravel-nullable-fields - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. iatstuti/laravel-nullable-fields

Abandoned → [dyrynda/laravel-nullable-fields](/?search=dyrynda%2Flaravel-nullable-fields)Utility[Database &amp; ORM](/categories/database)

iatstuti/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.

4.6.0(1mo ago)105155.0k↑100%11MITPHPPHP ^8.3CI passing

Since Jun 18Pushed 1y ago3 watchersCompare

[ Source](https://github.com/michaeldyrynda/laravel-nullable-fields)[ Packagist](https://packagist.org/packages/iatstuti/laravel-nullable-fields)[ RSS](/packages/iatstuti-laravel-nullable-fields/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (32)Used By (0)

Nullable database fields for the Laravel PHP Framework
======================================================

[](#nullable-database-fields-for-the-laravel-php-framework)

[![Build Status](https://github.com/michaeldyrynda/laravel-nullable-fields/workflows/run-tests/badge.svg)](https://github.com/michaeldyrynda/laravel-nullable-fields/actions?query=workflow%3Arun-tests)[![Latest Stable Version](https://camo.githubusercontent.com/66ff757f88559e02f6fa1d130f11a83ae23859f08ac8906572afdae245e3b624/68747470733a2f2f706f7365722e707567782e6f72672f647972796e64612f6c61726176656c2d6e756c6c61626c652d6669656c64732f762f737461626c65)](https://packagist.org/packages/dyrynda/laravel-nullable-fields)[![Total Downloads](https://camo.githubusercontent.com/326b1921dbc0be6ea50071aaf9f5937bbc67625b97e5dfc04f2499532183f9d7/68747470733a2f2f706f7365722e707567782e6f72672f647972796e64612f6c61726176656c2d6e756c6c61626c652d6669656c64732f646f776e6c6f616473)](https://packagist.org/packages/dyrynda/laravel-nullable-fields)[![License](https://camo.githubusercontent.com/7bb6d3835a69386a516d770fb0605d40088b374f7a487aad6473f7ab6e037933/68747470733a2f2f706f7365722e707567782e6f72672f647972796e64612f6c61726176656c2d6e756c6c61626c652d6669656c64732f6c6963656e7365)](https://packagist.org/packages/dyrynda/laravel-nullable-fields)[![Buy us a tree](https://camo.githubusercontent.com/130148911f548b001b2ac68a32c0a06559977ca60ada3bf480c72ae4ea093175/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54726565776172652d2546302539462538432542332d6c69676874677265656e)](https://plant.treeware.earth/michaeldyrynda/laravel-nullable-fields)

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
============

[](#installation)

This trait is installed via [Composer](http://getcomposer.org/). 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.

```
