2017-02-12 50 views
0

我相信有更多的开发者需要使用区域日期格式(在应用程序前端显示),而不是那些在浏览器中使用默认格式(12/22/2016。Laravel - 为获取者和创建者设置3个函数

所以我在我的Laravel项目标准日期像created_at,的updated_at和deleted_at做了一个小特点:

<?php 

namespace App\Traits; 

use Carbon\Carbon; 

trait FormatDates 
{ 
    protected $localFormat = 'd.m.Y H:i'; 


    // save the date in UTC format in DB table 
    public function setCreatedAtAttribute($date) 
    { 
     $this->attributes['created_at'] = Carbon::parse($date); 
    } 

    // convert the UTC format to local format 
    public function getCreatedAtAttribute($date) 
    { 
     return Carbon::parse($date)->format($this->localFormat); 
    } 

    // get diffForHumans for this attribute 
    public function getCreatedAtHumanAttribute() 
    { 
     return Carbon::parse($this->attributes['created_at'])->diffForHumans(); 
    } 

    // save the date in UTC format in DB table 
    public function setUpdatedAtAttribute($date) 
    { 
     $this->attributes['updated_at'] = Carbon::parse($date); 
    } 

    // convert the UTC format to local format 
    public function getUpdatedAtAttribute($date) 
    { 
     return Carbon::parse($date)->format($this->localFormat); 
    } 

    // get diffForHumans for this attribute 
    public function getUpdatedAtHumanAttribute() 
    { 
     return Carbon::parse($this->attributes['updated_at'])->diffForHumans(); 
    } 

    // save the date in UTC format in DB table 
    public function setPublishedAtAttribute($date) 
    { 
     $this->attributes['published_at'] = Carbon::parse($date); 
    } 

    // convert the UTC format to local format 
    public function getPublishedAtAttribute($date) 
    {  
     return Carbon::parse($date)->format($this->localFormat); 
    } 

    // get diffForHumans for this attribute 
    public function getPublishedAtHumanAttribute() 
    { 
     return Carbon::parse($this->attributes['published_at'])->diffForHumans(); 
    } 

    // save the date in UTC format in DB table 
    public function setDeletedAtAttribute($date) 
    { 
     $this->attributes['deleted_at'] = Carbon::parse($date); 
    } 

    // convert the UTC format to local format 
    public function getDeletedAtAttribute($date) 
    {  
     return Carbon::parse($date)->format($this->localFormat); 
    } 

    // get diffForHumans for this attribute 
    public function getDeletedAtHumanAttribute() 
    { 
     return Carbon::parse($this->attributes['deleted_at'])->diffForHumans(); 
    } 

} 

有实际上只有3这些日期的功能,这些功能是:

set the date so it can be saved with date time picker 
get the date in locale format (22.12.2016 14:39) 
get the date in human readable format 

所以我的问题是如何使这个特质只有3个功能,而不是所有的时间重复它的每一个变量?这是可行的吗?

回答

1

您可以设置它类似于Custom setters and getters in Laravel

__get()/__set()在您的性状中的方法将在雄辩模型中的getXAttribute()/setXAttribute()方法之前调用。

您可以使用$this->getDates()获取每个模型的日期,并创建一个辅助方法来定义您应该在哪个日期字段上调用哪种方法。

尽管此解决方案需要的代码较少,但个人而言,我并没有发现FormatDates特征中具有特定访问器的特定访问器错误,看着可读性很差。

+0

因此,你所说的是,如果我把它留在可读性方面并没有那么差,即使它是更多的代码 – lewis4u

+0

当然,意见各不相同,但上面的代码是自解释性和高度可读的,即使它涉及重复的代码。当模型中的可用日期字段变化很大时,您可能需要考虑使用自定义__get/__ set解决方案,从而根据模型getDates()的输出使该特性更具动态性。如果日期字段在整个模型中相对统一,我个人认为目前的解决方案没有太多错误。 – Robert