temper/laravel-nullable-properties

This package is abandoned and no longer maintained. No replacement package was suggested.

1.0.1 2021-03-22 10:31 UTC

This package is auto-updated.

Last update: 2023-02-26 17:17:39 UTC


README

This package provides automatically nulled properties for Eloquent Models.

When patching an Eloquent Model, it might be common to do something like this:

$this->property = $request->get('property');

Or even:

$this->fill($request->all());

A side effect of this is that empty fields in forms are stored in the database as empty strings, even when the database column is nullable.

This can interfere with future queries such as:

$users = User::whereNull('favoriteColor')->get();

Usage

Usage is simple. Add:

  • The NullableProperties trait
  • An array named $nullable, containing Model property names (similar to arrays like $fillable and $casts):
<?php

use Illuminate\Database\Eloquent\Model;
use Temper\NullableProperties\NullableProperties;

class User extends Model {
    use NullableProperties;

    public $nullable = ['favoriteColor'];
};

Properties in this array are not allowed to be stored as empty strings, they will always default back to null when empty.

Laravel can not easily detect nullable values at runtime. but you can easily get a table's nullable properties from the database:

select concat('protected $nullable = [', group_concat(concat("'",column_name,"'")),'];')
from information_schema.columns
where table_name = 'users' and is_nullable = 'YES'
and data_type = 'varchar';