Skip to content

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
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.

2 participants