Skip to content

defaultWrap not work #946

Closed
Closed
@burasuk

Description

@burasuk

✏️ Describe the bug

I have "data" object in contoller that return data properly.
But when i try to create custom wrap name as documentation suggest, nothing changes.

↪️ To Reproduce

class ListProductController extends Controller
{

    public function __invoke(Request $request)
    {
        $products = Product::paginate();
        return ProductData::collect($products);

    }
}
class ProductData extends Data
{
  public function __construct(
    public string $name,
    public string $description,
    public string $reference,
  ) {
  }

  public function defaultWrap(): string
  {
    return 'products';
  }
}

reponse:

{
  "current_page": 1,
  "data": [ 
    {
      "name": "autem",
      "description": "Rerum rerum et id qui exercitationem incidunt corporis.",
      "reference": "9417542784724",
    }
]
// rest of properties
}

✅ Expected behavior

{
  "current_page": 1,
  "products": [ // <- THIS
    {
      "name": "autem",
      "description": "Rerum rerum et id qui exercitationem incidunt corporis.",
      "reference": "9417542784724",
    }
]
// rest of properties
}

🖥️ Versions

Laravel: 11.32
Laravel Data: 4.13
PHP: 8.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions