【发布时间】:2016-11-04 12:04:00
【问题描述】:
我有我的 VitalSignSet 模型:
class VitalSignSet extends Model
{
protected $dates = [
'datetimetaken',
];
. . .
}
现在在我的函数中,我有这个函数,它返回与最新生命体征集的 json 编码遭遇。 (遇到与 VitalSignSet 有 hasMany 关系)虽然在返回之前,我希望将 datetimetaken 字段格式化为人类可读性,但仅适用于这种特定方法。 (这就是我没有使用访问器的原因)
public function get(Request $request, Encounter $encounter) {
// Setting the latest vital sign set
$encounter->latest_vitals = $encounter->VitalSignSets()
->orderBy('datetimetaken','desc')->get()->first();
// Formatting the date :
// Works when just returning the date.
// Does not return in this format when returning the model with the date.
$encounter->lastest_vitals->datetimetaken->format('M j, Y');
return $encounter->toJson();
}
上面的方法是从一个js ajax请求中访问的。当我解析并记录响应时,datetimetaken 格式没有改变。 (仍然是YYYY-mm-dd H:i:s 格式)但是当我在格式化后只返回$encounter->latest_vitals->datetimetaken; 时,会以我设置的格式返回一个字符串。但是当我返回包含 VitalSignSet 模型$encounter->latest_vitals;(json 响应)时,格式为YYYY-mm-dd。这是为什么呢?
【问题讨论】:
标签: php laravel-5.3 php-carbon