joskolenberg/eloquent-empty-date-nullifier

Convert 0000-00-00 dates in the database to null when accessing your model's attributes.

v1.0 2019-09-04 05:42 UTC

This package is auto-updated.

Last update: 2024-04-04 16:21:25 UTC


README

Scrutinizer Code Quality Code Coverage Build Status Total Downloads Latest Stable Version License

Eloquent Empty Date Nullifier

Little trait to convert "0000-00-00" dates in the database to null when you access them using Eloquent models, useful when you have to deal with (legacy) polluted data.

Installation

composer require joskolenberg/eloquent-empty-date-nullifier

Usage

Add the JosKolenberg\EloquentEmptyDateNullifier\NullifiesEmptyDates trait to your model, and all your 0000-00-00 dates will be converted to null when accessing them using your eloquent model and during serialization.

use Illuminate\Database\Eloquent\Model;
use JosKolenberg\EloquentEmptyDateNullifier\NullifiesEmptyDates;

class Person extends Model
{
    use NullifiesEmptyDates;

    protected $dates = [
        'date_of_birth',
    ];
}

Example:

$person = new Person();

$person->setRawAttributes([
	'date_of_birth' => '0000-00-00',
]);

$person->date_of_birth; // = null

Notes

"0000-00-00" dates can be present in a database when empty values are stored but the column is not set to be nullable. The recommended approach is to make the column nullable and convert all the empty values to null in the database. However, this might not always be possible e.g. when other (external) applications rely on the same data. In that case this package may come in handy.

That's it! Any suggestions or issues? Please contact me!

Happy coding!

Jos Kolenberg jos@kolenberg.net