*/ class AnnouncementRepository extends BaseRepository implements AnnouncementRepositoryInterface { public function __construct(Announcement $model) { parent::__construct($model); } public function findBySlug(string $slug): ?Announcement { return $this->model->newQuery()->where('slug', $slug)->first(); } /** * @param array $filters * @return LengthAwarePaginator */ public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator { $query = $this->model->newQuery(); if (! empty($filters['category'])) { $query->where('category', $filters['category']); } if (! empty($filters['featured'])) { $query->where('is_featured', true); } if (! empty($filters['search'])) { $query->where(function ($q) use ($filters) { $q->where('title', 'like', '%'.$filters['search'].'%') ->orWhere('excerpt', 'like', '%'.$filters['search'].'%'); }); } $sortField = $filters['sort'] ?? '-published_at'; $direction = str_starts_with($sortField, '-') ? 'desc' : 'asc'; $sortField = ltrim($sortField, '-'); $allowedSorts = ['title', 'published_at', 'created_at']; if (in_array($sortField, $allowedSorts)) { $query->orderBy($sortField, $direction); } else { $query->latest('published_at'); } return $query->paginate($perPage); } }