Replies: 4 comments 5 replies
-
Took a look at the project we've created this package for. In this case we make a more 'traditional' split. So we have data objects being used for create and update without the id. And we have a resource object which is used for the show views with an id and probably a lot of other information which is not required with the data create/update views like the id. So each entity has 2 data objects: the real data object and a resource with a lot more info. Another solution you could look at is using inheritance, but maybe that will complicate things even more. |
Beta Was this translation helpful? Give feedback.
-
For what it's worth - we played a lot with it too, and in the end decided to split it and create separate Data objects for their individual contexts. Makes the validation rules much clearer, but of course still requires you to maintain all these Data objects in case you add an attribute to a model for instance. |
Beta Was this translation helpful? Give feedback.
-
I'm facing the same problem, at the moment I am using something like
For a create operation, both name and description are required, but for a "patch/update" its possible that only the name will be updated but not the description, so for ProductUpdateDto name and description are Optional. However in the real world these DTOs have about 30 properties each, and I think it will be very messy to duplicate the properties in two or potentially more classes. I also considered something like this
But this violates PHP inheritance, the child class is not allowed to change the property type from the parent. Happy to hear any suggestions on this topic. |
Beta Was this translation helpful? Give feedback.
-
I have the same issue and have been looking for answers in other frameworks |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there,
I'm trying to use the same data object for CRUD actions in my application and I feel like the pattern I'm using is a little awkward. In particular, dealing with
id
fields and ids of related models .id
attributeid
to be provided at allAt the moment I'm solving this by doing something like:
This way I can still use the model constructor to create data in my tests. However, I feel like this feels a little awkward and in some more complex models I find myself needing to add more attributes as optional and prohibited.
I feel like one approach could be to split the data model up into a read object, create object and update object. In the laracon video Freek showed an example of something like this with a data object at the path:
\App\Http\App\Requests\Project\CreateProjectData
.This makes it clearer that what is going to be validated and makes it easier to prevent some fields from being specified on update. However, this creates another problem which is potentially duplicating a lot of the validation rules with bigger objects.
How do you guys approach this problem?
Beta Was this translation helpful? Give feedback.
All reactions