public function batchNotify($type, User $fromUser, $users, Post $post, PostComment $comment = null)
{
foreach ($users as $key => $value) {
if ($fromUser->id == $key) {
continue;
}
$model = new Notification();
$model->setAttributes(['from_user_id' => $fromUser->id, 'user_id' => $key, 'post_id' => $post->id, 'comment_id' => $comment ? $comment->id : 0, 'data' => $comment ? $comment->comment : $post->content, 'type' => $type]);
$this->notifiedUsers[] = $key;
if ($model->save()) {
User::updateAllCounters(['notification_count' => 1], ['id' => $key]);
} else {
throw new Exception(array_values($model->getFirstErrors())[0]);
}
}
return count($this->notifiedUsers);
}