Question: I am using the [serialize](https://docs.djangoproject.com/en/3.0/topics/serialization/#djangojsonencoder) method to get a json of a Model object. However it does not work when I pass in a specific id.

```
data = serialize('json', Sample.objects.get(id=1), use_natural_foreign_keys=True)
print(data)

```

It seems to only work when you get the entire model's content by `all()` i.e.:

```
data = serialize('json', Sample.objects.all(), use_natural_foreign_keys=True)

```

Answer: 
You may try aggregating by `DATE_FORMAT` of your date, with the mask `%Y-%m`:

```
$results = Post::select(\DB::raw("DATE_FORMAT(created_at, '%Y-%m') AS ym, COUNT(*) AS cnt"))
->groupBy(\DB::raw("DATE_FORMAT(created_at, '%Y-%m')"))
->get();

```

If your actual database be Postgres, then a similar approach using `TO_CHAR` should work:

```
$results = Post::select(\DB::raw("TO_CHAR(created_at, 'YYYY-MM') AS ym, COUNT(*) AS cnt"))
->groupBy(\DB::raw("TO_CHAR(created_at, 'YYYY-MM')"))
->get();

```
here you go

```
return Post::select(DB::raw('count(1) AS count'), DB::raw('DATE_FORMAT(created_at, "01-%m-%Y") AS month'))->groupBy(function($post) { // Get all posts as collection and apply groupBy method
     $post->created_at->format('01-m-Y'); // ex: 01-02-2019
})->get()

```

I hope this will work for you...