人类可读时间戳Laravel 4带有数据透视表

时间:2014-05-12 22:58:39

标签: php laravel eloquent

如果在数据透视表中使用了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();
}  

3 个答案:

答案 0 :(得分:7)

想象一下,我们有两个模型: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'

答案 1 :(得分:2)

来自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函数应返回``class的实例,该类本身继承自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);
     }

}

答案 2 :(得分:1)

在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

相关问题