Skip to content

Using the fromModel method when the data collection is called #382

Discussion options

You must be logged in to vote

For anyone that might run into the same challenge, this is how you fix it:

final class AdminCategoryData extends Data
{
    public function __construct(
        #[DataCollectionOf(AdminComponentData::class)]
        public ?DataCollection $components,
    ) {
    }

    public function fromModel(RemoteCategory $category): self
    {
        return new self(
            AdminCategoryBrandData::collection($category->components->map(function ($component) {
                return AdminComponentData::fromModel($component);
            })),
        );
    }
}

You can send fromModel data objects to the collection, you just have to loop/map through them on the parent.
If any package developer see…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by matthiastjong
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants