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

[10.x] Allow default values when merging values into a resource #48073

Merged
merged 1 commit into from Aug 15, 2023
Merged

[10.x] Allow default values when merging values into a resource #48073

merged 1 commit into from Aug 15, 2023

Conversation

axlon
Copy link
Contributor

@axlon axlon commented Aug 15, 2023

This PR adds the option to enter a default when merging data into a resource. If the condition is not met and no default value is given a MissingValue instance will be returned, just like before.

Before:

class MyResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            /* other properties */
            $this->mergeWhen($this->hasSpecificData(), $this->specificData()),
            $this->mergeUnless($this->hasSpecificData(), static::genericData()),
        ];
    }
}

After:

class MyResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            /* other properties */
            $this->mergeWhen($this->hasSpecificData(), $this->specificData(), static::genericData()),
        ];
    }
}

@taylorotwell taylorotwell merged commit c658d14 into laravel:10.x Aug 15, 2023
21 checks passed
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.

None yet

2 participants