有沒有一種方法可以始終使用碳排序列返回日期?Laravel總是使用碳返回日期列
假設我有一個Challenges
模型/表與date_end
列。
當我做這個電話我ChallengesController
它返回JSON我的應用程序的所有challenges
:
public function index()
{
return response()->json([
'status'=>'success',
'challenges' => Auth::user()->challenges,
]);
}
但具體日期仍然是MySQL的格式。我知道我可以做:
public function index()
{
$challenges = Auth::user()->challenges;
foreach ($challenges as $challenge){
$challenge['humandate'] = $challenge->date_end->diffForHumans();
}
return response()->json([
'status'=>'success',
'challenges' => $challenges,
]);
}
然後通過challenge.humandate
獲取日期但有一個更清潔的方式?
https://laravel.com/docs/5.4/eloquent-mutators#date-mutators – castis