resources

Transform Eloquent models into tailored JSON responses using Laravel API Resources.

3|Updated Feb 13, 2026
One-click install
npx skills add https://github.com/codebar-ag/coding-guidelines --skill resources-codebar-ag
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: resources
Source: https://github.com/codebar-ag/coding-guidelines/tree/main/resources/boost/skills/resources
Command: npx skills add https://github.com/codebar-ag/coding-guidelines --skill resources-codebar-ag

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Developers need precise control over API payloads and to prevent overexposure of model data when building JSON responses.

Core Features & Use Cases

  • Whitelist and shape attributes exposed in API responses for Eloquent models.
  • Use relationships lazily with whenLoaded and conditional attributes with when/whenNotNull/mergeWhen for flexible responses.
  • Prefer dedicated Resource and ResourceCollection classes to keep controllers lean and support consistent pagination and formatting.

Quick Start

Create a UserResource for your model and return a filtered array of fields for the API response.

Frequently Asked Questions about resources

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I shape Laravel Eloquent models into JSON responses without exposing sensitive data?

Use Laravel API Resources to whitelist model attributes and transform Eloquent models into JSON, ensuring only specified fields are exposed and preventing unintended data leakage in your API payloads.

How do I conditionally load relationships in Laravel API Resource responses?

Use the whenLoaded method in your Laravel API Resource class. This ensures relationship data is only included in the JSON response when the relationship has already been explicitly loaded on the Eloquent model.

What is the best way to include conditional fields in Laravel JSON resources?

Use the when, whenNotNull, or mergeWhen methods within your Laravel API Resource class. These methods allow you to flexibly add attributes to your API response only when specific conditions are met.

Does Laravel API Resources handle pagination metadata automatically?

Dedicated ResourceCollection classes support consistent pagination and formatting, but this Skill specifically avoids generating manual pagination metadata, preferring to keep controllers lean and formatting consistent.

Why should I use dedicated Resource classes instead of returning Eloquent models directly from controllers?

Using dedicated Resource and ResourceCollection classes keeps controllers lean and provides precise payload control. This approach ensures consistent formatting and prevents overexposure of sensitive model attributes in JSON responses.