Vigyata.AI
Is this your channel?
GithubGithub00

19 Add whenHas to JsonResource

This PR adds whenHas() to JsonResource, and I like it because it solves a very specific API problem cleanly. If an attribute exists on the model, I can keep the key in the response even when the value is null, which makes resource output more predictable.

Buy on Github

You'll be taken to Github to complete your purchase.

Pros

  • +Lets me include a resource key only when the attribute exists on the model
  • +Keeps null keys in the JSON output when that’s what I actually want
  • +Supports passing a closure for the value

Cons

  • -Easy to confuse with whenNotNull() if you don’t understand the key-present vs value-present difference

Featured in this video