首页 > 解决方案 > GroupBy by each month

问题描述

I'm studying GroupBy I can display year around result using below code.

How do I display each month by 'created_at' column data?

Controller:

public function index()
{
    $branches = DB::table('contacts')
        ->select('branch','created_at')
        ->orderBy('created_at')
        ->groupBy(DB::raw(MONTH('created_at')))
        ->get();

    $ttl_c = Contact::count('id');

    return view('index', [
        'branches' => $branches,
        'ttl_c' => $ttl_c,
    ]);
 }

index.blade.php:

<table border="0">
@foreach($branches as $branches => $branch)
<tr>
    <td>{{ $branch->branch }}</td>
    <td>{{ $branch->branch_count }}</td>
</tr>
@endforeach
</table>
     

标签: laravel

解决方案


推荐阅读