2017 © Pedro Peláez
 

utility 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.

image

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.

  • Wednesday, February 14, 2018
  • by michaeldyrynda
  • Repository
  • 4 Watchers
  • 68 Stars
  • 62,479 Installations
  • PHP
  • 4 Dependents
  • 1 Suggesters
  • 6 Forks
  • 0 Open issues
  • 22 Versions
  • 10 % Grown

The README.md

Nullable database fields for the Laravel PHP Framework

Build Status Latest Stable Version Total Downloads License Buy us a tree, (*1)

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., (*2)

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., (*3)

Laravel does not currently support automatically setting nullable database fields as null when the value assigned to a given attribute is empty., (*4)

Installation

This trait is installed via Composer. To install, simply add it to your composer.json file:, (*5)

$ 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., (*6)

<?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., (*7)

<?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 *:, (*8)

class UserProfile extends Model
{
    use NullableFields;

    protected $nullable = '*';
}

More information

Working with nullable fields in Eloquent models - first iteration, (*9)

Working with nullable fields in Eloquent models - Part Deux - second iteration, covers the details of this package, (*10)

Support

If you are having general issues with this package, feel free to contact me on Twitter., (*11)

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., (*12)

If you're using this package, I'd love to hear your thoughts. Thanks!, (*13)

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., (*14)

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., (*15)

You can buy trees here, (*16)

Read more about Treeware at treeware.earth, (*17)

The Versions

14/02 2018

dev-master

9999999-dev

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/02 2018

1.6.0

1.6.0.0

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

31/08 2017

1.5.0

1.5.0.0

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

04/07 2017

1.4.1

1.4.1.0

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

27/01 2017

1.4.0

1.4.0.0

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

27/01 2017

dev-5.4-compatibility

dev-5.4-compatibility

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

12/01 2017

dev-#19-fix-incorrect-attribute-updates

dev-#19-fix-incorrect-attribute-updates

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

22/12 2016

1.3.1

1.3.1.0

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

24/08 2016

1.3.0

1.3.0.0

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

17/08 2016

1.2.2

1.2.2.0

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

09/08 2016

1.2.1

1.2.1.0

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

07/08 2016

1.2.0

1.2.0.0

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

22/05 2016

1.1.0

1.1.0.0

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

29/01 2016

1.0.2

1.0.2.0

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

26/01 2016

1.0.0

1.0.0.0

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.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/11 2015

0.1.6

0.1.6.0

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.

  Sources   Download

MIT

The Requires

 

01/10 2015

0.1.5

0.1.5.0

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.

  Sources   Download

MIT

The Requires

 

14/07 2015

0.1.4

0.1.4.0

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.

  Sources   Download

MIT

The Requires

 

18/06 2015

0.1.3

0.1.3.0

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.

  Sources   Download

MIT

The Requires

 

18/06 2015

0.1.2

0.1.2.0

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.

  Sources   Download

MIT

The Requires

 

18/06 2015

0.1.1

0.1.1.0

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.

  Sources   Download

MIT

The Requires

 

18/06 2015

0.1.0

0.1.0.0

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.

  Sources   Download

MIT

The Requires

  • php >=5.4.0