Feature Request: Merge from multiple payloads #651
regnerisch
started this conversation in
Ideas
Replies: 2 comments 2 replies
-
Hmm, I think this can be easily added to a project with a helper function on a trait. It is not something I'll want to add to the package, data objects have already a lot of methods. |
Beta Was this translation helpful? Give feedback.
1 reply
-
Maybe there is a better way but something like this worked for me... use Spatie\LaravelData\Data as BaseData;
abstract class Data extends BaseData
{
public function merge(array | BaseData $payload): static
{
if ($payload instanceof BaseData) {
$payload = $payload->toArray();
}
return $this->from([...$this->toArray(), ...$payload]);
}
} |
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.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm missing a merge function for merging from multiple payloads.
The idea is to create a data object from e.g. a model and overwrite/extend it with e.g. a request payload. So you can easily fill the data object form an existing model and overwrite just some fields with a given request and save it.
Edit:
The
merge
function in the given example would do the same asfrom
but merges the payload.Beta Was this translation helpful? Give feedback.
All reactions