所以我有这样的东西:
$comments = Comments::select('id', 'topic', 'parent_id', 'username', 'comment', 'upvotes', 'created_at', 'updated_at')
->where('topic', '=', $topic)
->get();
// I'd like to add a human readable datetime difference like
// 1 day ago, 2 days ago, etc. before I return the $comments variable here.
return json_encode($comments);
对于单行我会做这样的事情:
$comment->created_at->diffForHumans();
如何向所有行添加额外的属性,例如carbon_date?
最佳答案
循环遍历 $comments
数组并将属性添加到每个元素。
foreach ($comments as $comment) {
$comment->carbon_date = $comment->created_at->diffForHumans();
}
关于php - 如何在返回之前向数据库查询添加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47273661/