How can map database table fields with data objects ?? #49
-
I have created a Data object called InvoiceData like below.
And I have a create function like below.
The thing is like I use camel cases in the properties and use snake cases in the database table fields. How can I map these table fields and data objects ?? I just want to use one variable inside create() method. I use fromModel() and fromRequest() methods. But, I couldn't do this using these 2. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There isn't a proper solution to fix this, in your case, I would create a trait with a method At Spatie we're using snake case properties within our data objects because it clearly communicates to a developer that a property is part of a data object but that's off course personal preference. |
Beta Was this translation helpful? Give feedback.
There isn't a proper solution to fix this, in your case, I would create a trait with a method
toEloquentAttributes
here you can transform a data objecttoArray
and then replace the keys of the array with snakeCase versions of your camelCase keys.At Spatie we're using snake case properties within our data objects because it clearly communicates to a developer that a property is part of a data object but that's off course personal preference.