我无法将总计正确输出到事件列表中

时间:2019-03-27 22:51:24

标签: php laravel vue.js

我有一些事件,这些事件在它们的发生日期下具有该打印输出,但是我需要使用“今天+日期”或“昨天=日期”进行打印,具体取决于这是否正确。

    $notes = UsersNote::where('user_id','=',$user_id)->get();
    foreach($notes as $note) {
        $activity = [];
        $activity['icon'] = "/assets/icon-activity2-new-note.svg";
        $activity['action'] = "New Note";
        if($note->is_private > 0) {
            $activity['icon'] = "/assets/icon-activity2-new-private-note.svg";
            $activity['action'] = "New Private Note";
        }
        $activity['detail'] = $note->note;
        if(!empty($note->author_id)) {
            $author_name = UsersPermission::withTrashed()->where('user_id','=',$note->author_id)->value('name');
        }
        if(!empty($author_name)) $activity['detail'] = $author_name." wrote: ".$activity['detail'];
        $activity['media'] = "";
        $activity['link'] = "";
        $activity['time'] = strtotime($note->created_at);
        $activity['hourly'] = Format::shortTime($note->created_at);
        $activity['since'] = Format::shortDate($note->created_at);
        if($view == "all" && $note->is_private > 0) $activities[] = $activity;
        else if($note->is_private == 0) $activities[] = $activity;
    }

    // Re-order by date
    usort($activities, function ($a, $b) {
        return ($b['time'] > $a['time']);
    });


// Group
$new_activities = [];
foreach($activities as $activity) {
    if(isset($new_activities[$activity['since']])) $new_activities[$activity['since']][] = $activity;
    else $new_activities[$activity['since']] = [$activity];

    $the_date=strtotime($activity['since']);
    if($the_date = strtotime('today UTC')) {
        "Today ".$the_date;
    } elseif ($the_date = strtotime('yesterday UTC')) {
        "Yesterday ".$the_date;
    } else {
        $the_date;
    }

}
return response()->json(array_values($new_activities), 200);

1 个答案:

答案 0 :(得分:0)

假设$new_activities = [];是一个集合。 (如果它是将其转化为集合的数组collect($array)

您可以利用map()添加字段或覆盖现有字段。

$activities->map(function($activity) {
    $the_date = strtotime($activity['since']);

    if($the_date == strtotime('today UTC')) {
       $activity['formatted_date'] = "Today ". $the_date;
    } elseif ($the_date == strtotime('yesterday UTC')) {
       $activity['formatted_date'] = "Today ". $the_date;
    } else {
       $activity['formatted_date'] = $the_date;
    }

    return $activity;
})

现在用dd($activities)检查结果。

相关问题