Beyond Data aka laravel-data v4 #407
Replies: 9 comments 17 replies
-
@rubenvanassche what do you think about introducing the "scope" attribute (as tags for each property)? class InvoiceDataObject extends DataObject
{
public function __construct(
#[WithScope(['nano'])]
public string $id,
#[WithScope(['nano', 'micro'])]
public string $description,
#[WithScope(['micro'])]
public \DateTime $expiresAt,
)
{}
} Then for transformation: $dataObject = InvoiceDataObject::from(...)->transform(scope: ['nano'])
// or
$dataObject = InvoiceDataObject::from(...)->scope(['nano'])->transform() The result will be: [
"id" => "...",
"description" => "..."
] |
Beta Was this translation helpful? Give feedback.
-
It will be useful to support the customization of paginated collection transformation (by adapter or something like it) because at present it is hardcoded: protected function wrapPaginatedArray(array $paginated): array
{
$wrapKey = $this->wrap->getKey() ?? 'data';
return [
$wrapKey => $paginated['data'],
'links' => $paginated['links'] ?? [],
'meta' => Arr::except($paginated, [
'data',
'links',
]),
];
} For example, in our API we have this response structure at this moment. This is a little pain to transform laravel-data {
"success": true,
"data": [...],
"meta": {
"pagination": {
"total": 129,
"count": 100,
"per_page": 100,
"current_page": 1,
"total_pages": 2,
"links": {
"next": "https://.../v1/feed/events?page=2"
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
When writing tests I think it would be nice if there was an easy way to get a faked version of a DTO. For example, if you are creating DTOs from the Request then you would want a way to generate a DTO with faker data that you could then use in the test. Maybe it would look something like this package for creating fake FormRequests https://github.com/worksome/request-factories |
Beta Was this translation helpful? Give feedback.
-
It would be great to get support for read-only fields. My use-case is using a JSON column to store a blob of data - but when it is read, hydrating it into a useful DTO. This DTO could have methods to manipulate it's state, but to ensure that the state stays valid, it would be great to be able to support read-only properties. Maybe this could just be an attribute on the constructor fields which throws an exception when a write is detected to a field. |
Beta Was this translation helpful? Give feedback.
-
Something to think about: #470 |
Beta Was this translation helpful? Give feedback.
-
It lacks the ability to add additional fields to the collection output. That is, use the For example: class SongController
{
public function index()
{
return SongData::collection(Song::paginate())->additional(['key' => 'value']);
}
} The JSON will now look like this: {
"data" : [
{
"name" : "Never Gonna Give You Up",
"artist" : "Rick Astley"
},
{
"name" : "Giving Up on Love",
"artist" : "Rick Astley"
}
],
"key" : "value"
} |
Beta Was this translation helpful? Give feedback.
-
This would also be nice. |
Beta Was this translation helpful? Give feedback.
-
I think this is super important. In some situations, when dealing with a lot of data, Laravel Data (v3) has been a bottleneck so hearing it will be worse with v4 doesn't sound like good news :( Here's a recent example, there's a 4s difference when using |
Beta Was this translation helpful? Give feedback.
-
It is out, sadly enough not with all features we've wanted to implement but that's something for another release 😄 Blogpost: https://rubenvanassche.com/hi-there-laravel-data-4/ |
Beta Was this translation helpful? Give feedback.
-
We've started working on laravel-data v4, this will like v2 be a big one and reshape the package quite a bit:
What will be added or changed
Arrays, Collections and Paginators are now valid DataCollections
With v1 of this package we introduced
DataCollection
,PaginatedDataCollection
andCursorPaginatedDataCollection
. Though these classes will still exist, they are extremely handy to return a collection of data objects where you want to include certain properties.As from v4, you're allowed to use arrays, collections, ... within your data objects and store data objects within them. Magic from's, includes, excludes and all other features the package now provides will still keep working.
Addition of the collect method
Each data object will now have a
collect
method, this method will look a lot like thecollection
method we're having now. It creates data objects from the items in the collection passed to it. But there will be one difference, it will return the same collection type as provided.So providing an array of arrays to the collect method returns an array of data objects, same with Laravel collections and paginators. You'll even be able to convert collections, for example, an array to a DataCollection.
Removal of the collection method
With the addition of the
collect
method, thecollection
method becomes something strange in the new data objects, it will be a relic from the past and that's why we decided to remove it. You'll have to refactor your code to the newcollect
method. or you could include a trait which adds thecollection
back for an easy refactor, this trait will be removed in v5 so don't wait to long with refactoring your code!Magic data collections
We had already magic
from*
methods on data objects which magically created data objects, now we're also adding support to define magiccollect*
methods which allow you to define how a collection of data objects will be constructed.Rewritten partial trees resolving
Partial trees allow you to include, exclude, except and only certain properties in a tree of data objects. The original system dates back to v1 of the package where each data object would store what their partial trees were. This meant that including, for example, on nested data objects would be ignored. Since only the initial include would be used when transforming.
In v4, a global partial tree would be constructed without touching the individual data objects, this is a much more robust system which should have a lot of benefits in the future.
Dto and Resource
You've always defined data objects by extending from
Data
. From now on you will also be able to extend fromDto
a minimal data object which allows you to create and validate data objects and nothing more. It is basically a more light version of Data.We'll also introduce
Resource
, a data object which doesn't do validation. And can be used perfectly for resources sen't to your frontend.The original Data class will still exists and provides all functionality, but sometimes a more light object is required.
Smaller changes:
from
without parameters will be possible if you've already defined all the defaultsThe maybe list
These ideas could be implemented, I'm not entirely sure if we want to though:
prepareForValidation
method which would be called in the normalizers (Add Request validation data pipe #381)Todo list
Other features?
If you have any ideas for cool new features, or things which needs to look different. Then add them to the comments, we can't guarantee these things will end up in this release but
Testing
The
beyond-data
branch is already available, it is very work in progress so expect things to break. But you're always welcome to test drive it!Expected release
At the moment I have no idea, it will probably take a few more months before release. We'll keep on supporting and merging PR's to v3 for the moment, these then will be ported to v4.
PR's with breaking changes will be send to v4 so that we're having one big release with a lot of new functionality.
Beta Was this translation helpful? Give feedback.
All reactions