Return count: 0 if nothing was found

Posted 1 month ago by MartinZeltin

I am trying to group count items in my table but the thing is that if it does not find anything matching the criteria then it doesn't return 0 but instead skips it altogether. Is there a way for it to return 0 if it did not find anything?

$sms_count = SentItems::selectRaw('SenderID, count(*) as count')
                      ->whereIn('SenderID', ['modem1', 'modem2', 'modem3', 'modem4', 'modem5'])
                      ->whereBetween('SendingDateTime', [$date_from . ' 00:00:00', $date_to . ' 23:59:59'])
                      ->groupBy('SenderID')
                      ->get();

For example here it skipped modem1, modem3, modem4 instead of returning count => 0

  0 => 
  array (
    'SenderID' => 'modem2',
    'count' => 2,
  ),

  1 => 
  array (
    'SenderID' => 'modem5',
    'count' => 1,
  ),

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.