thisvessel/carbonated 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

thisvessel/carbonated

Composer 安装命令:

composer require thisvessel/carbonated

包简介

An Eloquent model trait that offers more flexible timestamp/date/time handling.

README 文档

README

DISCLAIMER: This is a work in progress! Use at your own risk! When I am happy with API and test coverage, I will tag version. Suggestions welcome :)

Build Status Coverage Status

An Eloquent model trait that offers flexible timestamp/date/time handling.

Eloquent provides DateTime handling through Date Mutators. However, it can be cumbersome having to set Accessors for custom DateTime formatting in your front end, and Mutators to correct custom DateTime formatting coming into your database. Also, time field handling, nullable fields and timezone conversion are non-existent. Carbonated aims to help you with these things.

Feature Overview

  • Automatic accessors and mutators for timestamp, date, and time fields.
  • Set output formatting once in your model.
  • No need to format() every attribute for output.
  • Carbon instances are still available when you need to format() output.
  • Timezone support with automatic conversion between database and front end.
  • Plays friendly with form generators that use model binding.

Requirements

Installation

Via Composer:

composer require 'thisvessel/carbonated:dev-master'

Note: I will tag version as soon I've added sufficient test coverage.

Basic Usage

Use Carbonated trait in your Eloquent model.

<?php namespace App;

use ThisVessel\Carbonated;

class ServiceOrder extends Model {

    use Carbonated;

}

Add timestamp, date, and time fields to their respective carbonated model properties.

public $carbonatedTimestamps = ['created_at', 'updated_at'];
public $carbonatedDates = ['required_by', 'completed_on', 'invoiced_on'];
public $carbonatedTimes = ['pickup_time'];

That's it! Accessors and mutators are automatically applied with sensible formatting for front end.

{{ $serviceOrder->created_at }}  // Outputs 'Jun 09, 2015 4:10pm'.
{{ $serviceOrder->required_by }} // Outputs 'Jul 30, 2015'.
{{ $serviceOrder->pickup_time }} // Outputs '10:30am'.

If you need access to raw carbon instances, the withCarbon attribute returns a clone of your object with carbon instances instead of formatted strings.

{{ $serviceOrder->withCarbon->required_by->format('M Y') }}

Customization

Customize view output format by adding these properties to your model.

public $carbonatedTimestampFormat = 'M d, Y g:ia';
public $carbonatedDateFormat = 'M d, Y';
public $carbonatedTimeFormat = 'g:ia';

Customize JSON output format by adding these properties to your model.

public $jsonTimestampFormat = 'Y-m-d H:i:s';
public $jsonDateFormat = 'Y-m-d';
public $jsonTimeFormat = 'H:i:s';

Customize database storage format by adding these properties to your model.

public $databaseTimestampFormat = 'Y-m-d H:i:s';
public $databaseDateFormat = 'Y-m-d';
public $databaseTimeFormat = 'H:i:s';

You can also override all automatic accessors and mutators by providing your own Accessor and Mutator methods in your model.

public function getRequiredByAttribute($value)
{
    return $value; // Returns raw value from database.
}

Timezone Conversion

Carbonated supports automatic timezone conversion between your database and front end. For example, maybe you are storing as UTC in your database, but want to output as America/Toronto.

You can set explicitly set timezones by adding these properties to your model.

public $carbonatedTimezone = 'America/Toronto';
public $jsonTimezone = 'UTC';
public $databaseTimezone = 'UTC';

If $carbonatedTimezone is not defined in your model, Carbonated will search for an authenticated user with a $timezone property. This allows the user model be responsible for user specific timezones.

public $timezone = 'America/Toronto;'

The above properties can be set dynamically using Accessors instead of explicit properties.

public function getTimezoneAttribute()
{
    return 'America/Toronto';
}

If either $carbonatedTimezone or $jsonTimezone are undefined, $databaseTimezone will be used as a fallback.

If $databaseTimezone is undefined, the app's timezone (found in /config/app.php) will be used as a fallback.

If you are using Carbonated with Eloquent outside of Laravel, $databaseTimezone will fallback to UTC.

统计信息

  • 总下载量: 403
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固