Repository navigation
Add typed UUID retrieval to InteractsWithData #61501
Replies: 1 comment
|
This makes sense conceptually, but I think the main design question may be the return type rather than the
Would it be worth considering returning Laravel's existing UUID value type instead, or otherwise keeping the parsing implementation behind a Laravel abstraction? Another option could be to make the behavior consistent with the existing I think the API would be useful; I'd mainly want to make sure the choice of UUID value object doesn't unnecessarily couple |
Uh oh!
There was an error while loading. Please reload this page.
Proposal
Laravel's
InteractsWithDatatrait already provides typed retrieval methods for common data types:Would it make sense for
InteractsWithDatato also provide typed UUID retrieval?With a return type such as:
Because the typed retrieval API lives on
InteractsWithData, this would not be limited toRequest; it would also be available consistently across validated input and other framework classes using the trait.Rationale
Laravel already provides strong UUID support across several layers of the framework:
HasUuids$table->uuid()Str::uuid()andStr::isUuid()However, at the data-entry boundary (
Request/InteractsWithData), developers are still left working with raw strings.For modern PHP codebases that favor strong typing and value objects, being able to convert incoming UUID data into a typed
UuidInterfaceat the boundary feels like a natural missing piece in that existing UUID support.Semantics & Design Considerations
One design question is how invalid values should behave.
The existing typed retrieval methods do not all use identical semantics:
integer()performs a tolerant cast.enum()relies ontryFrom()and may return a default value ornull.date()may throw when parsing an invalid value.One possible UUID behavior could be:
similar to
date(), although a more tolerant or fallback-oriented approach could also be considered.Next Steps
The main question is whether converting UUID input into a UUID value object fits the intended direction of Laravel's typed data retrieval API.
If the core team is open to this addition, I would be happy to submit a focused PR with implementation and tests.
All reactions