2017-11-13 10 views
0

は、だから私はこのようなものがあります:私はこのような何かをするだろう、単一の行については返す前にデータベースクエリに属性を追加するにはどうすればよいですか?

$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のようなものを余分に属性を追加することができますか?

答えて

0

ループを介して$commentsの配列を検索し、各要素に属性を追加します。

foreach ($comments as $comment) { 
    $comment->carbon_date = $comment->created_at->diffForHumans(); 
} 
+0

これは日付を提供しますが、追加の属性で$ comments変数をどのように更新するのですか? – rotaercz

+0

オブジェクトを適所に修正しますか?あなたはそれが1つの行のためにそれを行う方法だと言った。 – Barmar

+1

そうでない場合は、プロパティに割り当てます。 – Barmar

関連する問題