人类可读的时间戳Laravel 4与数据透视表


Human Readable Timestamps Laravel 4 with pivot tables

如果在数据透视表中使用created_at和updated_at时间戳,是否有办法以人类可读的格式显示它们?

我是新的整个数据透视表laravel的事情,但现在我使用一个数据透视表来存储用户评论的个别帖子,每个帖子都有created_at和updated_at时间戳。

如果我只是使用一个普通的表与一个模型,我将扩展我的基本模型,看起来像这样:

use Carbon'Carbon;
class Base extends Eloquent {
protected function getHumanTimestampAttribute($column)
{
    if ($this->attributes[$column])
    {
        return Carbon::parse($this->attributes[$column])->diffForHumans();
    }
    return null;
}
public function getHumanCreatedAtAttribute()
{
    return $this->getHumanTimestampAttribute("created_at");
}
public function getHumanUpdatedAtAttribute()
{
    return $this->getHumanTimestampAttribute("updated_at");
} 
}

但是由于我没有使用数据透视表的模型,我该如何解决这个问题?有没有一种方法可以让这些函数与我的数据透视表一起工作?使用数据透视表模型是最简单/正确的方法吗?

编辑这是我的关系的样子,它被放置在我的用户模型中。

/**
 * Guide comments
 *
 * @return mixed
 */
public function guide_comments()
{
    return $this->belongsToMany('Guide', 'guides_comment')->withTimestamps();
}  

假设我们有两个模型:UserCategory具有多对多关系,因此具有枢轴(category_user: id, user_id, category_id, created_at, updated_at)表要链接2。

现在,让我们这样设置关系:

// User model
public function categories()
{
   return $this->belongsToMany('Category')->withTimestamps();
}
// Category model
public function users()
{
   return $this->belongsToMany('User')->withTimestamps();
}

然后,由于时间戳在默认情况下被更改为Carbon对象,因此您需要做的就是:

$user = User::with('categories');
$user->categories->first()->pivot->created_at->diffForHumans();

你不需要自定义PivotModel,也不需要那些额外的访问器(它们非常令人困惑,而且一开始就完全是多余的)。


如果您想稍微简化一下,实际上可以这样定义访问器:

// same for both models
public function getPivotHumansCreatedAtAttribute()
{
    if (is_null($this->pivot)) return null;
    return $this->pivot->created_at->diffForHumans();
}
// then you access it like this:
Category::first()->pivotHumansCreatedAt; // null, since it's not called as relation
User::first()->categories()->first()->pivotHumansCreatedAt; // '2 days ago'

来自Laravel文档

http://laravel.com/docs/eloquent working-with-pivot-tables

定义自定义数据透视模型

Laravel还允许您定义自定义Pivot模型。要定义自定义模型,首先创建您自己的扩展Eloquent的"Base"模型类。在您的其他Eloquent模型中,扩展这个自定义基模型,而不是默认的Eloquent基模型。在基本模型中,添加以下函数,该函数返回自定义Pivot模型的实例:

 public function newPivot(Model $parent, array $attributes, $table, $exists)
 {
     return new YourCustomPivot($parent, $attributes, $table, $exists);
 }

newPivot函数应该返回从Illuminate'Database'Eloquent'Model类继承的"类的实例和类本身。由于Eloquent只是Illuminate'Database'Eloquent'Model的别名,我认为以下应该工作。

class YourCustomPivot extends Illuminate'Database'Eloquent'Relations'Pivot {
protected function getHumanTimestampAttribute($column)
{
    if ($this->attributes[$column])
    {
        return Carbon::parse($this->attributes[$column])->diffForHumans();
    }
    return null;
}
public function getHumanCreatedAtAttribute()
{
    return $this->getHumanTimestampAttribute("created_at");
}
public function getHumanUpdatedAtAttribute()
{
    return $this->getHumanTimestampAttribute("updated_at");
} 
}

现在修改基类。

class Base extends Eloquent {
     public function newPivot(Model $parent, array $attributes, $table, $exists)
     {
         return new YourCustomPivot($parent, $attributes, $table, $exists);
     }
}

在PHP中试试:

$date = '2014-04-30 19:49:36';  //date returned from DB
echo date("H:i:s",strtotime(str_replace('/','-',$date)));     // 19:49:36
echo date("Y/m/d",strtotime(str_replace('/','-',$date)));     // 2014/04/30

用于格式检查:http://www.php.net/manual/en/datetime.formats.date.php