Download the PHP package jcfrane/laravel-resource-scope without Composer
On this page you can find all versions of the php package jcfrane/laravel-resource-scope. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-resource-scope
Laravel Resource Scope
Control which fields your Laravel API Resources return based on context. Define scopes like listing, detail, or summary and let the frontend request only the data it needs.
Inspired by Symfony's serialization groups.
Requirements
- PHP 8.2+
- Laravel 11, 12, or 13
Installation
The package auto-discovers its service provider. No manual registration needed.
Publish Config (Optional)
Quick Start
1. Add the trait to your resource
2. Register the middleware
In bootstrap/app.php:
Or apply it to specific route groups:
3. Request scoped data
You can also pass the scope via header:
Defining Scopes
Method-based (recommended)
Define a scopeDefinitions() method that returns scope name => allowed field keys:
Attribute-based
Use PHP 8 attributes on the resource class:
If both are present, scopeDefinitions() takes priority.
Scope Cascading
When a resource contains nested resources, you can control how scopes propagate. There are two ways to define mappings:
Method-based mappings
Attribute-based mappings
You can also define mappings directly in the #[ResourceScope] attribute using the mappings parameter:
When the listing scope is active on PostResource, the nested UserResource will automatically use the summary scope.
If both scopeMappings() method and attribute mappings are present, the method takes priority. If no mapping is defined, the parent's scope name passes through to nested resources. If the nested resource doesn't define that scope, it returns all fields.
Works with Laravel's Conditional Fields
Scoping works alongside whenLoaded(), whenHas(), and when(). Both conditions apply — Laravel's conditional checks run first, then scoping filters the keys:
Backwards Compatible
- No scope parameter = all fields returned (existing behavior)
- Scope not defined on a resource = all fields returned
- Unknown scope name = all fields returned
No breaking changes to existing API responses.
Configuration
Published config file (config/resource-scope.php):
Testing
License
MIT
All versions of laravel-resource-scope with dependencies
illuminate/http Version ^11.0|^12.0|^13.0
illuminate/support Version ^11.0|^12.0|^13.0