joskolenberg / eloquent-empty-date-nullifier
当访问模型属性时,将数据库中的 "0000-00-00" 日期转换为 null。
v1.0
2019-09-04 05:42 UTC
Requires
- illuminate/database: ^5.5|^6.0
Requires (Dev)
- phpunit/phpunit: ^6.0|^7.0|^8.0
This package is auto-updated.
Last update: 2024-09-04 17:15:38 UTC
README
Eloquent Empty Date Nullifier
一个小特性,可以将数据库中的 "0000-00-00" 日期在通过 Eloquent 模型访问时转换为 null,当您需要处理(遗留)污染数据时非常有用。
安装
composer require joskolenberg/eloquent-empty-date-nullifier
用法
将 JosKolenberg\EloquentEmptyDateNullifier\NullifiesEmptyDates
特性添加到您的模型中,然后当您使用 Eloquent 模型和序列化访问这些日期时,所有 "0000-00-00" 日期都将被转换为 null。
use Illuminate\Database\Eloquent\Model; use JosKolenberg\EloquentEmptyDateNullifier\NullifiesEmptyDates; class Person extends Model { use NullifiesEmptyDates; protected $dates = [ 'date_of_birth', ]; }
示例
$person = new Person(); $person->setRawAttributes([ 'date_of_birth' => '0000-00-00', ]); $person->date_of_birth; // = null
注意
当空值存储在数据库中但列未设置为可空时,"0000-00-00" 日期可能存在于数据库中。建议的方法是将列设置为可空并将所有空值转换为 null。但是,这并不总是可能,例如,当其他(外部)应用程序依赖于相同的数据时。在这种情况下,这个包可能很有用。
就这么多!有任何建议或问题?请与我联系!
快乐编码!
Jos Kolenberg jos@kolenberg.net