Php 如何在透视表中设置日期列格式?
我在透视表的Laravel中遇到了日期字段格式的问题。 我想要实现的是使用Unix时间格式中的“created_at”和“updated_at”字段来代替时间戳。 我也试着使用一个定制的轴,但没有任何运气。到目前为止,代码工作正常,但它使用的是时间戳,而不是Unix 在我的代码中,我执行以下操作:Php 如何在透视表中设置日期列格式?,php,laravel,laravel-4,pivot-table,Php,Laravel,Laravel 4,Pivot Table,我在透视表的Laravel中遇到了日期字段格式的问题。 我想要实现的是使用Unix时间格式中的“created_at”和“updated_at”字段来代替时间戳。 我也试着使用一个定制的轴,但没有任何运气。到目前为止,代码工作正常,但它使用的是时间戳,而不是Unix 在我的代码中,我执行以下操作: //[.. other stuff ...] //update the relation wich references current user and group 1 $user->gr
//[.. other stuff ...]
//update the relation wich references current user and group 1
$user->groups()->sync(array(1 => array('newsletter' => $newsletter)));
这是我的代码:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;
class User extends Model{
public function groups() {
return $this->belongsToMany('Group')->withTimestamps();
}
public function newPivot(Eloquent $parent, array $attributes, $table, $exists) {
if ($parent instanceof Group) {
return new UserGroup($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
}
class Group extends Model {
public function users() {
return $this->belongsToMany('User')->withTimestamps();
}
public function newPivot(Eloquent $parent, array $attributes, $table, $exists)
{
if ($parent instanceof User) {
return new UserGroup($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
}
class UserGroup extends Pivot
{
protected function getDateFormat()
{
return 'U';
}
public function getDates()
{
return array(
'created_at',
'updated_at');
}
public function user() {
return $this->belongsTo('User');
}
public function group() {
return $this->belongsTo('Group');
}
}
感谢您的关注。您可能需要看看laravel文档中的突变和突变@fmgonzalezas如日期字段所述,您需要像我已经做的那样重写getDates()方法。以这种方式重写
getDates()
,您不会得到任何结果<$dates
数组中已包括在处创建的代码>和在处更新的代码。我认为您需要返回一个空数组,并创建您自己的mutators访问此字段以设置get Unixtimestamps。