/** * 批量处理通知 * @param $type * @param User $fromUser * @param $users * @param Post $post * @param PostComment $comment * @param null $content * @throws Exception */ public function batchNotify($type, User $fromUser, $users, Post $post, PostComment $comment = null, $content = 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' => $content ?: $comment->id, 'data' => $content ?: $comment->comment, 'type' => $type]); $this->notifiedUsers[] = $key; if ($model->save()) { User::updateAllCounters(['notification_count' => 1], ['id' => $key]); } else { throw new Exception(array_values($model->getFirstErrors())[0]); } } }