App\Http\Controllers\ArchiveController::groupByDate PHP Method

groupByDate() public method

Get all posts grouped by create date.
public groupByDate ( ) : Illuminate\Contracts\View\Factory | Illuminate\View\View
return Illuminate\Contracts\View\Factory | Illuminate\View\View
    public function groupByDate()
    {
        $posts = $this->post->all();
        $group = [];
        foreach ($posts as $post) {
            $date = date_parse($post->created_at);
            $mYear = $date['year'];
            $mMonth = $date['month'];
            // Month or Year not exists current.
            if (!array_key_exists($mYear, $group)) {
                $group[$mYear] = [];
            }
            if (!array_key_exists($mMonth, $group[$mYear])) {
                $group[$mYear][$mMonth] = [];
            }
            array_push($group[$mYear][$mMonth], $post);
        }
        return view('front.archive.date', compact('group'));
    }