Skip to content

How to return a collection in API Resource ? #2956

Discussion options

You must be logged in to vote

here is what worked for me, updating your code here

public function index(Showcase $showcase)
    {
        $showcases = $showcase->withTotalVisitCount()->paginate(25);

        return ShowcaseResource::collection($showcases);
    }

Now in ShowcaseResource

public function toArray($request): array|JsonSerializable|Arrayable
    {
        return [
            ```
            'cover_images' => MediaResource::collection($this->media),
            ```
        ];
    }

and now in MediaResource

public function toArray($request)
    {
        return [
            'id' => $this->id,
            'file_name' => $this->file_name,
            'size' => $this->size,
            'original_url' => $thi…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jameswong3388
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants