property case convention #21
Replies: 2 comments 1 reply
-
I'm not against using different conventions for property names since they make it very clear a property is part of a data object. A solution to this problem could be to overwrite the |
Beta Was this translation helpful? Give feedback.
-
I'm in the same situation. API resources and database (Eloquent models) use snake case, but I prefer camel case for properties. I ended up overriding the class Address extends Data
{
public function __construct(
public string $addressLine1,
public string|null $addressLine2 = null,
public string $locality,
public string $postalCode,
public string $countryCode,
) {}
public static function from($payload): static
{
$payload = [
'addressLine1' => $payload['address_line_1'],
'addressLine2' => $payload['address_line_2'],
'locality' => $payload['locality'],
'postalCode' => $payload['postal_code'],
'countryCode' => $payload['country_code'],
];
return parent::from($payload);
}
public function transform(TransformationType $type): array
{
$payload = parent::transform($type);
return [
'address_line_1' => $payload['addressLine1'],
'address_line_2' => $payload['addressLine2'],
'locality' => $payload['locality'],
'postal_code' => $payload['postalCode'],
'country_code' => $payload['countryCode'],
];
}
} This could probably be simplified with a custom snake case Would you recommend that approach? 🙂 On the other hand: The naming convention for Eloquent model attributes is already snake case. Perhaps this is just something to get used to. |
Beta Was this translation helpful? Give feedback.
-
Hey, Love the package.
Does anyone have any ways to deal with case? Our endpoints generally use snake_case for input and output,
So this makes us have to use
snake_case
properties in the dataSo now when using the data we access it via
$data->total_bar
and it makes us have inconsistent naming convention when most of our properties arecamelCase
.Does anyone have a strategy for dealing with this?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions