# Resources

# Use make

Avoid using new for single Resource and use make instead:

// bad
new UserResource($user);

// good
UserResource::make($user);

# Use $resource property

Use $resource for accessing model's properties

// Bad
[
    'id' => $this->id,
    'name' => $this->name,
]

// Good
[
    'id' => $this->resource->id,
    'name' => $this->resource->name,
]

# Relationships

Use resources for relationships

// Bad
[
    'name' => $this->name,
    'posts' => $this->posts
]

// Good
[
    'name' => $this->resource->name,
    'posts' => PostResource::collection($this->whenLoaded('posts')))
]

Always use conditions when attaching relationship attributes.

// Bad
[
    'name' => $this->name,
    'posts' => PostResource::collection($this->resource->posts))
]

// Good
[
    'name' => $this->resource->name,
    'posts' => PostResource::collection($this->whenLoaded('posts')))
]