Skip to content

Instantly share code, notes, and snippets.

@dwightwatson
Created June 18, 2015 05:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dwightwatson/0d3ccfcc447dbd0a7514 to your computer and use it in GitHub Desktop.
Save dwightwatson/0d3ccfcc447dbd0a7514 to your computer and use it in GitHub Desktop.
NullingTrait.php
<?php namespace App\Support;
trait NullingTrait
{
/**
* Boot the trait. Adds an observer class for nulling.
*
* @return void
*/
public static function bootNullingTrait()
{
static::saving(function($model) {
foreach($model->getAttributes() as $key => $value) {
if ($value === '') {
$model->setAttribute($key, null);
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment