Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
Next Chapter

Challenge #1 of 1


Q: 

Suppose we have a Product entity with the following category property:

#[ApiResource(normalizationContext: ['groups' => 'product:read']]
class Product
{
    // ...
    #[Groups('product:read')]
    public Category $category;
}

And Category looks like this:

#[ApiResource(normalizationContext: ['groups' => 'category:read']]
class Category
{
    // ...
    #[Groups('category:read')]
    public string $name;
}

Right now, when you make a GET request to /api/products/3, the response looks like this:

{
    "@id": "/api/products/3",
    "@type": "Product",
    "category": "/api/categories/5"
}

Instead of the IRI, what is the easiest way to turn the category field into an object with the category's name inside?

userVoice