首页 > 解决方案 > 在 laravel 中使用访问器的问题

问题描述

我需要使用两种不同类型的 created_at 字段

 public function getCreatedAtAttribute($value)
{
    $time = new \Verta($value);
    return $time->formatDifference();
}

这是我的代码,我也需要使用默认 created_at 值

请帮我

标签: laravel

解决方案


created_at_diff您可以尝试为您的特殊格式创建另一个具有不同名称(也许)的访问器:

public function getCreatedAtDiff()
{
    return (new \Verta($this->attributes['created_at']))->formatDifference();

    // or $this->created_at instead of $this->attributes['created_at']
}

然后您可以通过$model->created_at_diff.


推荐阅读