php - Laravel Eloquent, group by month/year -
i'm trying group data month , year.
$data ->select(db::raw('count(id) `data`'),db::raw('year(created_at) year, month(created_at) month')) ->groupby('year','month') ->get();
the output :
{ "data": 19215, "year": 2016, "month": 10 },
if group month, don't know year belong month, expected output :
{ "clicks": 19215, "month": 11-2016, }, { "clicks": 11215, "month": 12-2016,
},
i want in sql, not in php.
you can try as:
->select(db::raw('count(id) `data`'), db::raw("date_format(created_at, '%m-%y') new_date"), db::raw('year(created_at) year, month(created_at) month')) ->groupby('year','month') ->get();
Comments
Post a Comment