torzer/laravel-datetime-mutator
Composer 安装命令:
composer require torzer/laravel-datetime-mutator
包简介
Laravel traits mutators to help Date Time manipulation on Eloquent Models
README 文档
README
Laravel traits mutators to help Date Time manipulation on Eloquent Models.
Installing
composer require torzer/laravel-datetime-mutator
Traits
MapDateTimeMutator
The MapDateTimeMutator trait is used to set a date and time mutator (only date or time if necessary)
related to a specific input format.
WHen using this trait it's not necessary to implement a setVariableAttribute($value) mutator to transform a date from a format to another, WHen using this trait it's not necessary to implement a setVariableAttribute($value) mutator to transform a date from a format to another, as for example from d/m/Y format of a string or object to another that will be used to persist the date in database.
To use it, set in the class:
<?php use Torzer\Common\Traits\MapDateTimeMutator; class MyClass extends Model { use MapDateTimeMutator; ..
Set the date fields as you would do in array $dates, but to those dates or timestamps that need to be transformed
from one format to another, use the array $mapDateTimeMutator with the
name of the date field as the array key and an array mapping from and to formats:
<?php use Torzer\Common\Traits\MapDateTimeMutator; class MyClass extends Model { use MapDateTimeMutator; protected $mapDateTimeMutator = [ 'start_date' => ['from' => 'd/m/Y', 'to' => 'Y-m-d'], 'finish_date' => ['from' => 'd/m/Y', 'to' => 'Y-m-d'] ]; protected $dates = [ 'approved_at', 'start_date', 'finish_date' ]; ...
At the example above, the fields start_date and finish_date gonna be handle with the DateTime function of Laravel Eloquent Model,
but they arecreated from format d/m/Y set in from key of the $mapDateTimeMutator array,
getting as return/setAttribute of the field a string formated using the to key.
The approved_at field in $dates array is still handled with the default behavior of the framework.
Note that the input value of this fields - start_date and finish_date, must be in d/m/Y format.
Using Jenssegers/MongoDB
If you are using Jenssegers/MongoDB driver to persist date only, this trait can help you to handle MongoDB timestamp field type.
To ensure a date gonna be saved in UTC timezone at hour 00:00:00, you must add a date-only
setting in array $mapDateTimeMutator:
<?php use Torzer\Common\Traits\MapDateTimeMutator; class MyClass extends Jenssegers\Mongodb\Eloquent\Model { use MapDateTimeMutator; protected $mapDateTimeMutator = [ 'start_date' => ['from' => 'd/m/Y', 'to' => 'Y-m-d'], 'finish_date' => ['from' => 'd/m/Y', 'to' => 'Y-m-d'], 'date-only' => true, ]; protected $dates = [ 'approved_at', 'start_date', 'finish_date' ]; ...
torzer/laravel-datetime-mutator 适用场景与选型建议
torzer/laravel-datetime-mutator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 377 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 08 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 torzer/laravel-datetime-mutator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 torzer/laravel-datetime-mutator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 377
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-01