Conditional attributes and relationships in Laravel Resources

1 · Amit Merchant · July 6, 2019, midnight
When you’re building an Laravel application which uses APIs at some point, it’s no brainer to use Laravel’s resource classes as it provides the transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application’s users. In this article, I’m specifically going to talk about conditional attributes that can be used only include an attribute in a resource response if a given condition is met. So, as you may know each resource contains a ...