public function findByUser(User $user, $limit = null, $offset = 0) { $primaries = Notification::select(DB::raw('MAX(id) AS id'), DB::raw('SUM(is_read = 0) AS unread_count'))->where('user_id', $user->id)->whereIn('type', array_filter(array_keys(Notification::getTypes()), [$user, 'shouldAlert']))->where('is_deleted', false)->groupBy('type', 'subject_id')->orderBy('time', 'desc')->skip($offset)->take($limit); return Notification::with('subject')->select('notifications.*', 'p.unread_count')->mergeBindings($primaries->getQuery())->join(DB::raw('(' . $primaries->toSql() . ') p'), 'notifications.id', '=', 'p.id')->orderBy('time', 'desc')->get(); }
public function getUnreadNotificationsCount() { $types = array_keys(Notification::getTypes()); return $this->notifications()->whereIn('type', array_filter($types, [$this, 'shouldAlert']))->where('time', '>', $this->notification_read_time ?: 0)->where('is_read', 0)->count(\DB::raw('DISTINCT type, subject_id')); }