1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Laravel + api platform and snake case

Discussão em 'Outras Linguagens' iniciado por x-y-z-select, Outubro 1, 2024 às 09:23.

  1. x-y-z-select

    x-y-z-select Guest

    I'm using Laravel and API platform (https://api-platform.com/).

    By Laravel convetion and mysql convetion column names in table should be snake_case, but API platform requires camelCase. I found this

    In API Platform, if your entities are using snake_case for variable names, but the framework expects camelCase by default, this mismatch can cause issues. By default, API Platform uses the Symfony Serializer, which expects properties in camelCase.


    I tried to add this

    #[ApiResource(
    operations: [
    new GetCollection(
    normalizationContext: [
    'groups' => ['Brand:list'],
    'name_converter' => CamelCaseToSnakeCaseNameConverter::class,
    ],
    ),
    new Get(
    normalizationContext: [
    'groups' => ['Brand:read'],
    'name_converter' => CamelCaseToSnakeCaseNameConverter::class,
    ],
    )
    ],
    normalizationContext: [
    'groups' => ['Brand:read'],
    'name_converter' => CamelCaseToSnakeCaseNameConverter::class,
    ],
    denormalizationContext: [
    'groups' => ['Brand:write'],
    'name_converter' => CamelCaseToSnakeCaseNameConverter::class,
    ]
    )]


    But that is not working. In my table I have one column called local_name, and when I try to GET collection, I get all field but not that one. Did anyone else found solution how to get column names with snake_case. Also I have a getter getLocalName that returns $this->local_name, but it is not working. I'm missing something.

    Continue reading...

Compartilhe esta Página