morphMany(Attachment::class, 'attachmentable') ->ordered(); } /** * 특정 컬렉션의 첨부파일 조회 * * @param string $collection 컬렉션명 * @return Collection */ public function getAttachments(string $collection = 'default'): Collection { return $this->attachments() ->collection($collection) ->get(); } /** * 첫 번째 첨부파일 조회 * * @param string $collection 컬렉션명 * @return Attachment|null 첫 번째 첨부파일 또는 null */ public function getFirstAttachment(string $collection = 'default'): ?Attachment { return $this->attachments() ->collection($collection) ->first(); } /** * 특정 컬렉션의 이미지만 조회 * * @param string $collection 컬렉션명 * @return Collection */ public function getImages(string $collection = 'default'): Collection { return $this->attachments() ->collection($collection) ->images() ->get(); } /** * 첨부파일 개수 조회 * * @param string|null $collection 컬렉션명 (null이면 전체) * @return int 첨부파일 개수 */ public function getAttachmentsCount(?string $collection = null): int { $query = $this->attachments(); if ($collection !== null) { $query->collection($collection); } return $query->count(); } /** * 첨부파일이 있는지 확인 * * @param string|null $collection 컬렉션명 (null이면 전체) * @return bool 첨부파일 존재 여부 */ public function hasAttachments(?string $collection = null): bool { return $this->getAttachmentsCount($collection) > 0; } }