Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use isAbstractClassCast in DataCollectionEloquentCast for easy override #914

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

bentleyo
Copy link
Contributor

@bentleyo bentleyo commented Dec 24, 2024

I'm using abstract data classes in a way that's different to the eloquent-cast functionality of laravel-data (related to the problem I described in my old example PR #304).

The approach I have now is different / cleaner as I move from V2 to V3, but I need to be able to disable the abstract class functionality in DataCollectionEloquentCast and I'd prefer not to duplicate the code in the casts if possible!

@bentleyo
Copy link
Contributor Author

bentleyo commented Dec 24, 2024

If you're curious, what I'm doing is using properties inside the DTO to describe which concrete class / variant of the abstract class to use (as per #304). I've got support for creation from request payloads (with validation), singular data classes, data collections and nesting.

An example use case is having Vehicle as an abstract data class with type (to determine which concrete to use) and a common passengers property. Each concrete can then add additional properties e.g.:

"vehicles": [
    { "type": "horse", "max_passengers": 1, "name": "Bill" },
    { "type": "car", "max_passengers": 5, "transmission": "manual" },
]

It's something that's been super useful for us and a feature I think would be really great to have built-in. If you think this is a feature you would consider including in the package I'm happy to put together something to share what I've got (outside this PR of course) 😄

While I understand theres some support for abstract classes with eloquent casting I find the current implementation very limiting. It's only available for eloquent casting so can't be used elsewhere and for validation etc.


Update: I've created a PR for this here: #921

@bentleyo bentleyo marked this pull request as ready for review December 24, 2024 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant