【发布时间】:2014-06-30 11:30:31
【问题描述】:
如果在数据透视表中使用 created_at 和 updated_at 时间戳,有没有办法以人类可读的格式显示它们?
我对整个数据透视表 laravel 很陌生,但现在我正在使用数据透视表将用户 cmets 存储在各个帖子上,并且每个帖子都有 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();
}
【问题讨论】: