定制 jlorente/laravel-eloquent-splitted-dates-trait 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jlorente/laravel-eloquent-splitted-dates-trait

Composer 安装命令:

composer require jlorente/laravel-eloquent-splitted-dates-trait

包简介

A Laravel Trait for Eloquent Models to handle date fields that are stored both in the date field itself and in separate field components such as year, month, day, time, etc...

README 文档

README

A Laravel Trait for Eloquent Models to handle date fields that are stored both in the date field itself and in separate field components such as year, month, day, hour, etc...

Installation

The preferred way to install this extension is through composer.

With Composer installed, you can then install the extension using the following commands:

$ php composer.phar require jlorente/laravel-eloquent-splitted-dates-trait

or add

...
    "require": {
        "jlorente/laravel-eloquent-splitted-dates-trait": "*"
    }

to the require section of your composer.json file.

Usage

Basic Configuration

To enable and configure splitted dates fields in an Eloquent Model use the Jlorente\Laravel\Eloquent\Concerns\SplittedDates\HasSplittedDates Trait on the model and define the splittedDates array with the date fields you want to store splitted.

<?php

use Illuminate\Database\Eloquent\Model;
use Jlorente\Laravel\Eloquent\Concerns\SplittedDates\HasSplittedDates;

class Subscription extends Model
{
    use HasSplittedDates;

    /**
     * The splitted dates attributes.
     *
     * @var array
     */
    protected $splittedDates = [
        'begin_at'
    ];
}

In the example, the "begin_at" date will be stored splitted in the fields "begin_at_year", "begin_at_month" and "begin_at_day". Everytime the "begin_at" field is set, the other fields will be set also and vice versa.

$subscription = new Subscription();
$subscription->begin_at = Carbon::create(2019, 10, 2);

echo $subscription->begin_at_year; // 2019
echo $subscription->begin_at_month; // 10
echo $subscription->begin_at_day; // 2

If one of the splitted components is null, the main attribute "begin_at" correspondent component will be set to to 1 in case of month and day properties, 0 in case of time properties or the current year in case of year property.

$subscription = new Subscription();
$subscription->begin_at_year = 2025 // 2025

echo $subscription->begin_at->toDateTimeString(); // 2025-01-01 00:00:00

$subscription->begin_at_day = 26 // 2025

echo $subscription->begin_at->toDateTimeString(); // 2025-01-26 00:00:00

All the attributes defined in the splittedDates array will be treated as defined in the date mutator property so you can set its value to a UNIX timestamp, date string (Y-m-d), date-time string, or a DateTime / Carbon instance.

$subscription = new Subscription();
$subscription->begin_at = Carbon::create(2019, 10, 2); // OK
$subscription->begin_at = '2019-10-02' // OK
$subscription->begin_at = '2019-10-02 22:11:45' // OK
$subscription->begin_at = strtotime() // OK

Remember that the database table must have the correspondent columns "_year", "_month" and "_day" to store the splitted date value.

Advanced Configuration

The default configuration for splitted dates is to map the date value to a year, month and day field, but you can map the value to whatever carbon unit you want (year, month, day, hour, minute, second, milli, millisecond, micro, microsecond).

To configure the splitted date with other units than the default ones define which ones do you want to store on splittedDates property definition.

<?php

use Illuminate\Database\Eloquent\Model;
use Jlorente\Laravel\Eloquent\Concerns\SplittedDates\HasSplittedDates;

class Subscription extends Model
{
    use HasSplittedDates;

    /**
     * The splitted dates attributes.
     *
     * @var array
     */
    protected $splittedDates = [
        'begin_at' => ['hour', 'minute']
    ];
}

Doing so, the properties "begin_at_hour" and "begin_at_minute" will be set everytime the "begin_at" field is set and vice versa.

$subscription = new Subscription();
$subscription->begin_at = Carbon::create(2019, 10, 2, 15, 45, 12);

echo $subscription->begin_at_hour; // 15
echo $subscription->begin_at_minute; // 45

$subscription->begin_at_minute = 31;

echo $subscription->begin_at->toDateTimeString(); // 2019-10-02 15:31:12

Further considerations

The trait does not validate dates when setting the splitted fields, so it is possible that date overflows ocurr if you set an invalid value for a date.

$subscription = new Subscription();
$subscription->begin_at = Carbon::create(2019, 10, 2, 15, 45, 12);

$subscription->begin_at_hour = 27; // => Invalid value

echo $subscription->begin_at_hour; // 27
echo $subscription->begin_at->toDateTimeString(); // 2019-10-03 03:45:12

License

Copyright © 2019 José Lorente Martín jose.lorente.martin@gmail.com.

Licensed under the BSD 3-Clause License. See LICENSE.txt for details.

jlorente/laravel-eloquent-splitted-dates-trait 适用场景与选型建议

jlorente/laravel-eloquent-splitted-dates-trait 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.95k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「laravel」 「datetime」 「dates」 「day」 「Month」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jlorente/laravel-eloquent-splitted-dates-trait 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jlorente/laravel-eloquent-splitted-dates-trait 我们能提供哪些服务?
定制开发 / 二次开发

基于 jlorente/laravel-eloquent-splitted-dates-trait 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.95k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2019-10-02