Difference between this package and data-transfer-object package #18
-
Feature wise I can see the differences. This package is much more feature filled. What factors would you consider when deciding between the data-transfer-object package and this one? Or is this kind of like the next generation data-transfer-object package? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I was just wondering if I should move to this package and have the same question. It seems to superseed the DTO package but doesn't rely on it. Is there any reason to still use the DTO package with the exception of not needing the extra features of this one? |
Beta Was this translation helpful? Give feedback.
-
Like all the Spatie packages this package originates from one of our projects. Initially this actually wasn't a data package but only a resource object that would replace the Laravel Api Resources. We needed a custom implementation because we use the Typescript transformer package to convert these resources into Typescript definitions. Initially we actually used the spatie/data-transfer-object package for these resources. But then we fell in love with PHP 8's promoted properties. At that point the requirement for the spatie/data-transfer-object was dropped because we could just create object fairly easy! Then @brendt (the creator of the spatie/data-transfer-object) suggested the idea to validate data using attributes and this package grew from a resource only package to a complete dynamic data solution for Laravel in months. We still have old data-transfer-objects within that project, we now mostly convert these to plain PHP objects when we're working with them. When we find data-transfer-objects that almost completely map with resources then we use this laravel-data package. That doesn't answer your question at all but it gives an insight why we have these two similar packages. I would use this package when you're working with data coming from the frontend in controllers etc. When you want to be able to quickly validate data, and convert it to JSON with the nice ability to convert the structure to Typescript. When you're using data structures within your PHP project which have nothing to do with frontend then I would use plain PHP objects. And if I need a little bit more functionality from the objects then I would use the data-transfer-object package or this package 😄. We're still gonna keep supporting the data-transfer-object package and I think it's a great option for various cases! I'm also thinking about adding some support for data-transfer-objects casts etc in this package. But that's for later. |
Beta Was this translation helpful? Give feedback.
Like all the Spatie packages this package originates from one of our projects. Initially this actually wasn't a data package but only a resource object that would replace the Laravel Api Resources. We needed a custom implementation because we use the Typescript transformer package to convert these resources into Typescript definitions.
Initially we actually used the spatie/data-transfer-object package for these resources. But then we fell in love with PHP 8's promoted properties. At that point the requirement for the spatie/data-transfer-object was dropped because we could just create object fairly easy! Then @brendt (the creator of the spatie/data-transfer-object) suggested the idea to v…