Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Laracast13's avatar

Laravel Counting Related Models with date

I have list of users and showing how many Product they have.

User::withCount('products')->orderBy('products_count')->get()

I want add show product count with specific date period , tried this but not works

User::withCount('products')
->whereBetween('buy_date', array('2021-05-19 00:00:00', '2021-05-19 23:59:59'))
->orderBy('products_count')
->get()
0 likes
15 replies
Laracast13's avatar

I want show User with product count with time period . for ex. how many product user have last month

MichalOravec's avatar

Have you ever opened the documentation?

Because the answer is there...

Laracast13's avatar

can you help find where I have error?

$users = User::withCount([ 
       'products' => function ($query) {
           $query->whereBetween('buy_date', ['2021-06-19 00:00:00', '2021-07-19 23:59:59']);
       }])
       ->orderBy('products_count',  'desc')
       ->get();
bugsysha's avatar
bugsysha
Best Answer
Level 61

Try following:

\App\Models\User::query()
        ->withCount([
            'products' => function (\Illuminate\Database\Eloquent\Builder $query) {
                $query->whereBetween('buy_date', ['2021-05-19 00:00:00', '2021-05-19 23:59:59']);
            }
        ])
        ->orderBy('products_count')
        ->get();
2 likes
Laracast13's avatar

how can i use it in function, it not works like this

   public function statistics()
   {
  
      $users = \App\Models\User::query()
        ->withCount([
            'products' => function (\Illuminate\Database\Eloquent\Builder $query) {
                $query->whereBetween('buy_date', ['2021-05-19 00:00:00', '2021-05-19 23:59:59']);
            }
        ])
        ->orderBy('products_count')
        ->get();
 

       return view('statistics', ['users'=>$users ]);
   }
Laracast13's avatar

$users = User::withCount([ 
        'products' => function ($query) {
            $query->whereBetween('buy_date', ['2021-05-25 00:00:00', '2021-05-25 23:59:59']);
        }])
        ->orderBy('products_count',  'desc')
        ->get();

,


         @foreach($users as $user)    
                      <tr>
                          <td>{{$user->name}}</td>  
                      </tr> 
                      @endforeach

when I using foreach it showing first result from this date 2021-05-25 00:00:00', '2021-05-25 23:59:59 and after this showing all results. It must show only whereBetween date , but showing all user

Laracast13's avatar

It showing no result

$users = User::withCount([ 
        'products' => function ($query) {
            $query->whereBetween('buy_date', ['2021-05-25 00:00:00', '2021-05-25 23:59:59']);
        }])
->having('products_count')
        ->orderBy('products_count',  'desc')
        ->get();
Laracast13's avatar

forks like this ->having('products_count', '>' , 0)

Please or to participate in this conversation.