Replies: 1 comment 1 reply
-
|
Hi @Ruben10-R, Yeah this is on my list for a v5 release, yet I have no idea how we'll build a simple API to create such a thing. Feel free to provide some ideas! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
✏️ Describe the bug
When creating a data object, there can be a mismatch between input coming from a request and input coming from a model. Spatie Laravel Data currently has no way to differentiate whether a data object is being filled from a request or from a model, and how mapping should be handled in each case.
Example:
CandidateData->firstName
ApiCandidate->firstname (model)
CandidateRequest->first_name (request input)
Spatie Data offers #[MapInputName], but this cannot differentiate between firstname (model) and first_name (request). Manual mapping for each field is possible but becomes cumbersome when many fields and data objects need mapping. Also the
↪️ To Reproduce
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Data;
#[MapName(SnakeCaseMapper::class)]
abstract class AbstractApiData extends Data
{
// Simplified abstract class
}
class CandidateData extends AbstractApiData
{
#[MapInputName('firstname')]
public ?string $firstName;
}
// Example of request data
$requestData = [
'first_name' => 'John',
'last_name' => 'Doe',
];
// This will not map 'first_name' to 'firstName' correctly for model data
dd(CandidateData::from($requestData));
✅ Expected behavior
The first_name from the request should map correctly to CandidateData->firstName, while firstname from the model should also map correctly. It should be possible to define both mappings using a single attribute or optional parameters.
🖥️ Versions
Laravel: 12.x
Laravel Data (Spatie): 4.x
PHP: 8.4
Beta Was this translation helpful? Give feedback.
All reactions