I
I
Ivan2020-08-25 10:41:06
Laravel
Ivan, 2020-08-25 10:41:06

How to count the most active users who wrote posts for the current week?

Good afternoon. The situation is something like this. There is a posts table, in this table, among other things, there are user_id and post. It is necessary to somehow calculate the 3 most active users for the current week in descending order. Something like userX - 20 posts, userU - 15 posts, userZ - 3 posts. How approximately can this be done? The current week is counted by created_at.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2020-08-25
@Djonson86

User::withCount(['posts' => function($query) {
         $query->whereDate('created_at', '>=', now()->startOfWeek());
      }])
      ->orderByDesc('posts_count')
      ->take(3)
      ->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question