[9.x] Add whenNotNull method#41769
Merged
taylorotwell merged 6 commits intoApr 1, 2022
Merged
Conversation
Contributor
Author
|
Another thing that I realized during the development, is that maybe could be handy to include the Macroable trait in the JsonResource class. In this way, developers could make packages in order to easily extend them. In my case I create another JsonResource base class, that extends the original one, with the trait included, so I was able to reuse the method above through all my Resources: JsonResource::macro('whenSelected', function ($column, $default = null) {
$args = func_num_args() == 1 ?
[$column] :
[$column, $default];
return $this->when(!is_null($column), ...$args);
}); |
Member
|
Should it be a bit more explicitly named? Like just |
Contributor
Author
|
Sure thing! I'll do it right now. What about the Macroble trait? I think that the community would benefit from that. |
whenSelected method
Member
|
JsonResource is already macroable via the DelegatesToResource trait (which uses Macroable). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a new whenSelectedMethod to the ConditionallyLoadsAttributes trait. This is basically a syntactic sugar around the when method. Working with the spatie query builder, I found that I was iterating the same checks for every resource, so maybe this little wrapper could be helpful to maintain the logic inside the Resource as clean as possible.