如果在枢轴表中使用created_at和updated_at时间戳,它们是否可以以人类可读的格式显示?
我对整个枢轴表laravel这件事很陌生,但现在我使用pivot表来存储个人帖子上的用户评论,每个帖子都有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();
}
发布于 2014-05-13 16:15:27
假设我们有两个模型:User
和Category
,它们具有多到多的关系,因此有支点(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'
发布于 2014-05-13 06:36:21
从拉勒维尔的文件里。
http://laravel.com/docs/eloquent#working-with-pivot-tables
定义自定义轴心模型
Laravel还允许您定义自定义数据透视模型。要定义自定义模型,首先要创建自己的“基本”模型类,该类可以扩展雄辩。在其他有说服力的模型中,扩展这个定制的基本模型,而不是默认的雄辩基。在基本模型中,添加以下函数,该函数返回自定义数据透视模型的实例:
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);
}
}
发布于 2014-05-13 03:17:36
在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
https://stackoverflow.com/questions/23620279
复制相似问题