首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有枢轴表的人读时间戳Laravel 4

带有枢轴表的人读时间戳Laravel 4
EN

Stack Overflow用户
提问于 2014-05-12 22:58:38
回答 3查看 3.5K关注 0票数 5

如果在枢轴表中使用created_at和updated_at时间戳,它们是否可以以人类可读的格式显示?

我对整个枢轴表laravel这件事很陌生,但现在我使用pivot表来存储个人帖子上的用户评论,每个帖子都有created_at和updated_at时间戳。

如果我只是使用一个普通的表和一个模型,我会扩展我的基本模型,如下所示:

代码语言:javascript
运行
复制
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");
} 

}

但是,由于我没有使用模型作为枢轴表,我该如何处理这个问题呢?有什么方法可以让这些函数与我的枢轴表一起工作吗?对枢轴表使用模型是最简单/正确的方法吗?

编辑--这就是我的关系,这是放在我的用户模型中的。

代码语言:javascript
运行
复制
/**
 * Guide comments
 *
 * @return mixed
 */

public function guide_comments()
{
    return $this->belongsToMany('Guide', 'guides_comment')->withTimestamps();
}  
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-05-13 16:15:27

假设我们有两个模型:UserCategory,它们具有多到多的关系,因此有支点(category_user: id, user_id, category_id, created_at, updated_at)表来连接2。

现在,让我们来建立这样的关系:

代码语言:javascript
运行
复制
// User model
public function categories()
{
   return $this->belongsToMany('Category')->withTimestamps();
}

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

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

代码语言:javascript
运行
复制
$user = User::with('categories');

$user->categories->first()->pivot->created_at->diffForHumans();

您不需要自定义PivotModel,也不需要那些额外的访问器(这些访问器非常混乱,而且一开始就完全多余)。

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

代码语言:javascript
运行
复制
// 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'
票数 8
EN

Stack Overflow用户

发布于 2014-05-13 06:36:21

从拉勒维尔的文件里。

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

定义自定义轴心模型

Laravel还允许您定义自定义数据透视模型。要定义自定义模型,首先要创建自己的“基本”模型类,该类可以扩展雄辩。在其他有说服力的模型中,扩展这个定制的基本模型,而不是默认的雄辩基。在基本模型中,添加以下函数,该函数返回自定义数据透视模型的实例:

代码语言:javascript
运行
复制
 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的别名,所以我认为下面的工作应该是可行的。

代码语言:javascript
运行
复制
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");
} 

}

现在修改基类。

代码语言:javascript
运行
复制
class Base extends Eloquent {
     public function newPivot(Model $parent, array $attributes, $table, $exists)
     {
         return new YourCustomPivot($parent, $attributes, $table, $exists);
     }

}
票数 2
EN

Stack Overflow用户

发布于 2014-05-13 03:17:36

在PHP中尝试一下:

代码语言:javascript
运行
复制
$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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23620279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档