Download the PHP package denizgolbas/laravel-auto-relational-resources without Composer
On this page you can find all versions of the php package denizgolbas/laravel-auto-relational-resources. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download denizgolbas/laravel-auto-relational-resources
More information about denizgolbas/laravel-auto-relational-resources
Files in denizgolbas/laravel-auto-relational-resources
Package laravel-auto-relational-resources
Short Description Laravel package for automatically handling relational resources
License MIT
Informations about the package laravel-auto-relational-resources
This package automatically resolves relational data in Laravel Resource classes.
Installation
Publish the configuration file:
Usage
Using the Trait (Recommended)
Use the HasRelationalResources trait in your existing Resource classes:
Using AutoRelationalResource Class
Alternatively, you can extend your Resource class from AutoRelationalResource:
The package automatically detects loaded relations and returns them using the corresponding Resource classes.
Example Response
When you use the package with loaded relations, the response will automatically include related resources:
Controller:
Response:
As you can see, the package automatically:
- ✅ Includes the
postscollection relation - ✅ Includes the
profilemodel relation - ✅ Recursively includes nested relations like
commentsandauthor - ✅ Uses the corresponding Resource classes (
PostResource,ProfileResource,CommentResource, etc.)
Collection Response Example
When using Resource Collections, relations are automatically resolved for each item:
Controller:
Response:
Important Notes
File Structure Requirement: Collection and Resource files must follow the same path structure as your model files. The package converts the model class namespace to the Resource namespace to find the corresponding Resource class.
Examples:
- Model:
App\Models\User→ Resource:App\Http\Resources\UserResource - Model:
App\Models\Product\Category→ Resource:App\Http\Resources\Product\CategoryResource - Model:
App\Models\Order→ Collection:App\Http\Resources\OrderCollection(same rule applies for Collections)
If a Resource class is not found, the relation data will not be included.
Depth Control: By default, the package resolves all loaded relations with infinite depth. If you want to control the depth, you can use the max_depth configuration setting. If max_depth is null or 0, infinite depth is allowed (default). Set the max_depth value to limit the depth level.
Configuration
After publishing the configuration file, you can customize the package behavior. The configuration file is located at config/auto-relational-resources.php.
Available Configuration Options
model_namespace
Default: App\Models
The namespace where your Eloquent models are located. The package uses this to resolve Resource class names from Model class names.
Example:
resource_namespace
Default: App\Http\Resources
The namespace where your Resource classes are located. The package converts Model namespaces to Resource namespaces using this setting.
Example:
How it works:
- Model:
App\Models\User→ Resource:App\Http\Resources\UserResource - Model:
Domain\Models\Product→ Resource:Domain\Http\Resources\ProductResource
auto_load_relations
Default: true
When set to true, relations are automatically merged into the resource response. Set to false to disable automatic relation loading.
Example:
max_depth
Default: null (infinite depth)
Controls the maximum depth level for resolving relations. Set to null or 0 for infinite depth (default). Use a positive integer to limit the depth.
Examples:
Depth levels explained:
- Depth 0: Main resource
- Depth 1: Direct relations of the main resource
- Depth 2: Relations of relations
- Depth 3: Relations of relations of relations
allowed_empty_collections
Default: []
An array of collection relation names that should be included in the response even if they are empty. By default, empty collections are excluded.
Example:
This ensures that even if these collections are empty, they will appear in the response as empty arrays.
Environment Variables
You can also configure these settings using environment variables in your .env file:
Configuration Example
Here's a complete configuration example:
Features
- ✅ Automatic relation resolution
- ✅ Model and Collection support
- ✅ Depth control (prevents infinite loops)
- ✅ Configurable namespaces
- ✅ Empty collection management
License
MIT
All versions of laravel-auto-relational-resources with dependencies
illuminate/support Version ^10.0|^11.0
illuminate/http Version ^10.0|^11.0