【问题标题】:Laravel - Make only 3 functions for getters and settersLaravel - 只为 getter 和 setter 制作 3 个函数
【发布时间】:2017-02-12 13:50:44
【问题描述】:

我相信,与在浏览器中使用默认格式(即 2016 年 12 月 22 日)的开发者相比,需要使用区域设置日期格式(显示在应用前端)的开发者更多。

所以我在我的 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 个函数,而不是一直为每个变量重复它​​?这可行吗?

【问题讨论】:

    标签: php laravel date


    【解决方案1】:

    您可以将其设置为类似于Custom setters and getters in Laravel

    你的 trait 中的 __get() / __set() 方法将在 eloquent 模型中的 getXAttribute() / setXAttribute() 方法之前被调用。

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

    尽管此解决方案所需的代码更少,但就可读性而言,我个人认为在 FormatDates 特征中使用特定的访问器和修改器并没有什么问题。

    【讨论】:

    • 所以你的意思是,如果我这样写就可读性而言,这并不是那么糟糕,即使它是更多的代码
    • 意见当然各不相同,但上面的代码是不言自明且高度可读的,即使它涉及重复的代码。当模型中的可用日期字段变化很大时,您可能需要考虑使用自定义 __get/__set 解决方案,从而根据模型'getDates() 的输出使特征更加动态。如果您的模型中的日期字段相对统一,我个人认为当前解决方案没有太大问题。
    猜你喜欢
    • 2018-03-11
    • 1970-01-01
    • 2014-03-19
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    相关资源
    最近更新 更多