我有我的VitalSignSet型號:Laravel 5.3碳日期格式不會在退貨時設置?
class VitalSignSet extends Model
{
protected $dates = [
'datetimetaken',
];
. . .
}
現在在我的功能我有這個函數返回了最新的生命體徵的一組JSON編碼相遇。 (Encounter與VitalSignSet有許多關係)在返回之前,我希望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請求訪問的。當我解析並記錄響應時,日期時間格式沒有改變。 (仍然在YYYY-mm-dd H:i:s
格式)但是,格式化後我只返回$encounter->latest_vitals->datetimetaken;
,字符串以我設置的格式返回。但是當我返回包含VitalSignSet模型$encounter->latest_vitals;
(json響應)時,格式爲YYYY-mm-dd
。這是爲什麼?