【发布时间】:2014-08-28 00:07:35
【问题描述】:
我的应用中有一个数据透视表 ('offer_usage_detail'),该表有 4 个字段,如下所示
id int AutoIncrement
user_id int reference to user table
offer_id int reference to offer table
use_date date-time Store date time when user use offer
我需要以d-m-Y H:i 格式显示使用过优惠的用户列表。
所以我在我的报价模型中添加了以下代码
public function user() {
return $this->belongsToMany('User', 'offer_usage_detail', 'offer_id', 'user_id')->withPivot('use_time');
}
目前我正在使用核心 php 的 date 函数和 strtotime 来格式化日期时间,但我想知道有没有办法将数据透视表的日期时间字段转换为碳对象。
我尝试将use_time 字段放入Offer Model 的protected $dates = array('created_at','use_time');,但没有成功。
如果列是日期时间字段,是否可以将额外的数据透视表列转换为碳对象?
【问题讨论】:
标签: php laravel laravel-4 eloquent