bigsinoos/j-eloquent 问题修复 & 功能扩展

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

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

bigsinoos/j-eloquent

Composer 安装命令:

composer require bigsinoos/j-eloquent

包简介

Convert eloquent date attributes to jalali (Persian) dates on the fly. (supports model propery access, toJson, toString and toArray).

README 文档

README

Deprecation Notice

For newer versions of Laravel just use the simple attribute casting functionality which is a really nicer approach.

What is j-Eloquent?

Thanks to Laravel, Django and Rails we all know that convention over configuration (CoC) makes the development more funny. So suppose that you want to convert the Gregorian date attributes of your Eloquent models to Jalali (persian) dates, in this case j-Eloquent helps you to convert them conventionally, for example when you access a property named $model->jalali_created_at on your model, the PersianDateTrait detects the convention automatically and tries to convert created_at property of your model if it is a date attribute. This is also true for $model->toJson(); and $model->toArray(); fields.

Installation

require following line in your composer require secion :

	"require" : {
			// Other dependecies ,
			"bigsinoos/j-eloquent" : "dev-master" // Laravel 5 , "1.0" for Laravel 4
	}

Requirement

this package requires : miladr/jalali

Features

  • Coneventionally converts Eloquent model date attributes to Jalali date when the original date attribute is accessed prefixed by a string like jalali_.
  • Automatically converts date attributes to Jalali dates when model's toArrayl, __toString(); and toJson are called.
  • Custom date formats can be set for dates.

Documentation

The PersianDateTrait :

By using \Bigsinoos\JEloquent\PersianDateTrait trait in your models you can enable the plugin :

<?php

class Role extends \Eloquent {
    use \Bigsinoos\JEloquent\PersianDateTrait;
    protected $table = 'roles';
}

Usage

By default you can access your eloquent date attributes in jalali date by adding a jalali_ substring to the begining of your original attribute like jalali_created_at :

    $userPremiumRole = Auth::user()->roles()->where('type', 'premium');
    $userPremiumRole->create_at; // 2014/12/30 22:12:34
    $userPremiumRole->jalali_created_at; // 93/09/08
Changing jalali_ prefix

You can change the jalali date convention prefix with overriding $model->setJalaliFormat($format) and $model->getJalaliFormat(); or by overrriding $model->jalaliDateFormat property on your model class :

class Role extends \Eloquent {

    use \Bigsinoos\JEloquent\PersianDateTrait;

    protected $jalaliDateFormat = 'l j F Y H:i';
}

# or

class Role extends \Eloquent {
    
    use \Bigsinoos\JEloquent\PersianDateTrait;
    
    public function setJalaliFormat($format){
        // do custom things here
        $this->jalaliDateFormat = $format; return $this;
    }
    
    protected function getJalaliFormat()
    {
        // return any format you want
        return 'l j F Y H:i';
    }

}
Custom date attributes :

You can tell Eloquent that which one of your fields are date attributes like created_at and updated_at, then Eloquent treats them like Carbon objects you define multiple date attributes like this :

Class Role extends \Eloquent {
    
    use \Bigsinoos\JEloquent\PersianDateTrait;
    
    /**
    * Add this method to customize your date attributes
    *
    * @return array
    */
    protected function getDates()
    {
        return ['created_at', 'updated_at', 'expired_at'];
    }
    
}

When using the above trait all of the fields that are treated like date objects by Laravel will be available for conventional converting. They will be also added to model's toJson() , toArray(); and __toString(); methods.

Converter helper method

The $model->convertToPersian($attribute, $format); method allowes you to normally convert one of your fields, from Gregorian date to Jalali date :

$user = Auth::user();
$user->convertToPersian('created_at', 'y/m/d'); // 93/09/08

bigsinoos/j-eloquent 适用场景与选型建议

bigsinoos/j-eloquent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.69k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2014 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 bigsinoos/j-eloquent 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4.69k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 22
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 21
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: GPL
  • 更新时间: 2014-09-21