filters($request, [ Filter::search('q', 'Поиск', ['title', 'body'], 'заголовок или текст'), Filter::filled('read', 'Прочитано', 'да', 'нет', 'read_at'), Filter::dates('when', 'Когда', 'created_at'), ])->sortable(['id'], 'id'); $query = UserNotification::query()->where('user_id', $viewer->id()); return $this->view('notifications', [ 'active' => 'notifications', 'page' => $filters->apply($query)->paginate($this->page($request), $this->perPage()), 'unread' => UserNotification::unreadFor($viewer->id()), 'filters' => $filters, ], 'Уведомления'); } /** * Отметить одно прочитанным и уйти по ссылке уведомления, если она есть. */ public function read(int $id, Viewer $viewer): Response { $notification = $this->own($id, $viewer); $notification->markRead(); $url = trim((string) $notification->raw('url')); return $url !== '' ? Response::redirect($url) : $this->redirect('notifications'); } public function readAll(Viewer $viewer): Response { $count = UserNotification::markAllRead($viewer->id()); $this->flash($count > 0 ? 'Прочитано: ' . $count : 'Непрочитанных не было'); return $this->redirect('notifications'); } public function delete(int $id, Viewer $viewer): Response { $this->own($id, $viewer)->delete(); $this->flash('Уведомление убрано'); return $this->redirect('notifications'); } /** * Своё уведомление; чужое — «не найдено», а не «нет доступа». */ private function own(int $id, Viewer $viewer): UserNotification { /** @var UserNotification|null $notification */ $notification = UserNotification::query() ->where('id', $id) ->where('user_id', $viewer->id()) ->first(); /** @var UserNotification $found */ $found = $this->require($notification, 'notifications', 'Уведомление не найдено'); return $found; } }