Laravel API Resource Generator
Generate Laravel JsonResource and ResourceCollection classes with field mappings, conditional relations, and metadata.
- Home
- > Web Dev > Laravel Tools >
- API Resource Generator
Singular CamelCase, e.g. Post, User, Product
Auto-filled as {Model}Resource.
Leave empty for no wrapping.
Map each API response key to an expression. Supports $this->attribute, $this->relation->field, and helpers.
Relations that should only appear when they have been eager-loaded (whenLoaded).
Optional JSON object for the with() method. Used in collection for pagination links/meta.
Generated Resource
How to Use the Laravel API Resource Generator
This tool helps you create Laravel API Resource and Resource Collection classes. Follow these steps:
- Enter the model name — Type a singular CamelCase name like
Post. The resource class name auto-fills. - Define field mappings — Add rows mapping each API response key to a
$this->attributeor expression. - Add conditional relations — Use
whenLoadedto include relations only when eager-loaded. - Configure metadata — Add
with()metadata and pagination links for collections. - Generate — Click Generate Resource to create the PHP class(es).
- Copy or Download — Click Copy or Download to save as
{Model}Resource.php.
Frequently Asked Questions
What is an API Resource in Laravel?
API Resources are classes that transform your Eloquent models and collections into JSON responses. They live in app/Http/Resources/ and provide a clean way to shape API output, hide attributes, include relationships, and add metadata.
What is the difference between Resource and ResourceCollection?
Resource (extends JsonResource) transforms a single model instance. ResourceCollection (extends ResourceCollection) transforms a collection of models, often with pagination links.
What does whenLoaded() do?
whenLoaded('relation') includes the field only if the relation has been eager-loaded. This prevents N+1 queries while still allowing the resource to include relation data when available.
Should I wrap my resource in a data key?
Laravel 11 no longer wraps resources by default. If your API client expects a data wrapper (common in JSON:API), set the Wrap Key to data. Leave it empty for no wrapping.