【问题标题】:Human Readable Timestamps Laravel 4 with pivot tables带有数据透视表的人类可读时间戳 Laravel 4
【发布时间】: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();
}  

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    假设我们有 2 个模型: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'
    

    【讨论】:

    • 谢谢你,你已经清理了我似乎丢失的位!
    • 你能看看我更新的问题吗?我怎么能用这种类型的关系做到这一点?
    • 没有区别,只是在我的例子中引用guide_comments而不是categories
    【解决方案2】:

    来自 Laravel 文档。

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

    定义自定义透视模型

    Laravel 还允许你定义一个自定义的 Pivot 模型。要定义自定义模型,首先创建您自己的扩展 Eloquent 的“基础”模型类。在您的其他 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");
    } 
    
    }
    

    现在修改你的 Base 类。

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

    【讨论】:

      【解决方案3】:

      在 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

      【讨论】:

      • 我想他想用 DRY 的方式来做。
      猜你喜欢
      • 2019-03-15
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 2016-07-05
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多