Download the PHP package mustafafares/laravel-selective-response without Composer
On this page you can find all versions of the php package mustafafares/laravel-selective-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-selective-response
Laravel Selective Response
Automatic API resource filtering based on model select() queries with optional Scramble documentation support.
Features
- Automatic Filtering: Automatically filters API resource responses based on
select()queries - Zero Breaking Changes: Just change your resource parent class - no code changes needed
- Scramble Integration: Optional extension that updates API documentation to show only selected fields
- Flexible Configuration: Enable/disable globally or per-resource
- Always-Include Fields: Support for fields that should always be included
- Relationship Support: Works seamlessly with
whenLoaded()pattern
Installation
Publish the configuration file:
Quick Start
1. Update Your Resources
Change your resources to extend BaseApiResource instead of JsonResource:
2. Use in Controllers
That's it! The filtering happens automatically.
Usage Examples
Basic Usage
Always Include Fields
Disable Filtering
With Relationships
Dynamic Always-Include
Scramble Extension
The package includes an optional extension for Scramble that automatically detects select() calls in your controllers and updates the API documentation to show only selected fields.
Setup
-
Install Scramble (if not already installed):
-
Install PHP Parser (required for the extension):
-
Publish Scramble config (if not already done):
- Register the extension in
config/scramble.php:
How It Works
The extension uses PHP Parser to analyze your controller methods and find select() calls. It then filters the OpenAPI schema to show only the selected fields in the documentation.
Before Extension:
After Extension:
Disable Scramble Extension
You can disable the Scramble extension in the config:
Configuration
Publish the configuration file:
Available Options
Common Patterns
List Endpoint (Efficient)
Detail Endpoint (Full Data)
Search with Dynamic Fields
Force Full Response
Advanced Features
Custom Always-Include Logic
Conditional Computed Fields
Testing
Troubleshooting
Issue: All fields still returned
Solution: Verify you're extending BaseApiResource, not JsonResource
Issue: Relationships not working
Solution: Use $this->whenLoaded() for relationships
Issue: Computed fields missing
Solution: Use $this->whenAttributeLoaded() for computed fields
Issue: Need specific field always
Solution: Add to protected $alwaysInclude = ['field']; in your resource
Issue: Scramble extension not working
Solutions:
- Clear config cache:
php artisan config:clear - Verify extension is in
config/scramble.php - Check PHP Parser is installed:
composer show nikic/php-parser - Ensure Scramble is installed:
composer show dedoc/scramble
Performance Benefits
| Endpoint | Without Select | With Select | Improvement |
|---|---|---|---|
| User list | 50 KB | 10 KB | 80% smaller |
| Search | 100 KB | 20 KB | 80% smaller |
| Summary | 2 KB | 0.5 KB | 75% smaller |
Result: Faster APIs, lower bandwidth costs, better UX!
Migration Guide
From JsonResource to BaseApiResource
-
Change the parent class:
-
Add the import:
- That's it! Your
toArray()method works exactly the same.
Requirements
- PHP 8.1+
- Laravel 10.0+, 11.0+, or 12.0+
- (Optional) Scramble 1.0+ for documentation extension
- (Optional) PHP Parser 5.0+ for Scramble extension
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For issues and questions, please open an issue on GitHub.
All versions of laravel-selective-response with dependencies
illuminate/support Version ^10.0|^11.0|^12.0
illuminate/http Version ^10.0|^11.0|^12.0