Download the PHP package sourcetoad/enhanced-resources without Composer
On this page you can find all versions of the php package sourcetoad/enhanced-resources. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sourcetoad/enhanced-resources
More information about sourcetoad/enhanced-resources
Files in sourcetoad/enhanced-resources
Package enhanced-resources
Short Description Laravel's API Resources enhanced.
License MIT
Informations about the package enhanced-resources
Enhanced Resources
Enhancements for Laravel's API resources.
Installation
Basic Usage
To create an enhanced resource you simply extend Sourcetoad\EnhancedResources\Resource
instead of Illuminate\Http\Resources\Json\JsonResource
and provide a format method.
Formatting
With EnhancedResources you can have multiple formats for a single resource by adding format methods. Format methods are defined using the #[Format]
attribute.
If only a single format method is defined, as is the case in the example above in the basic usage section, that format will be the default format that is used when resolving the resource. However, you can define as many formats as you like.
In cases like the one above you'll need to specify the format to be used by providing its name to the format()
method. By default the format uses the same name as the method, so in this example we have format names of bar
, foo
, and foobar
.
Failing to specify the format in a situation where there is no default format will result in a NoFormatSelectedException
being thrown.
Specifying a Default
If you don't want to always explicitly specify the format to be used when you have a resource with multiple formats you can specify one format as default using the #[IsDefault]
attribute.
After adding the #[IsDefault]
attribute to one of your format methods it will be used unless the format is explicitly specified via the format()
method.
Specifying more than one default method via the #[IsDefault]
attribute will result in a MultipleDefaultFormatsException
being thrown.
The #[IsDefault]
attribute is detected on a per-class basis up the inheritance chain, so you can define a format as #[IsDefault]
on a parent resource and override it with another #[IsDefault]
format on the child resource without triggering a MultipleDefaultFormatsException
. However, if no #[IsDefault]
format is defined on the child resource the one on the parent will still be used.
Naming Formats
You can also override the name of formats and even provide multiple names for a single format. Let's look at the following example:
In this example we have three formats, but six names:
- The
bar
method can be used with the namesbar
, anda
. - The
foo
method can be used with the namesfoo
,b
, andsomething-else
. - The
foobar
method can be used with the namec
.
The primary name of each format is the first instance of the #[Format]
attribute, and the rest are aliases. This means that the primary names would be: bar
, foo
, and c
in the example above. In most cases this distinction should not come into play.
Collections
Both anonymous collections and defined resource collections utilize the formats of the underlying resource objects, and follow all the same rules.
Modifications
Modifications allow you to tweak the output of resources on the fly. They are applied similarly to how state
is applied for Eloquent factories. The most basic form of modification is a simple array merge modification done by providing an array to the modify
method of a resource:
To accomplish more complex modifications you can also pass any callable that accepts (array $data, Resource $resource)
. It is important when using these types of modifications to return the data as failing to do so will result in resource's data being replaced with null
.
You can also define methods on the resource class itself that can make modifications via calling the modify
method.
Except
The except enhancement is a modification class and trait combination that allows for the easy exclusion of certain fields from a resource.
Only
The only enhancement is a modification class and trait combination that allows for the easy exclusion of certain fields from a resource.
Additional Enhancements
EnhancedResources also includes a couple of other helpful enhancements.
Status Codes
You can now tweak the status code of the resource response with a simple call to the setResponseStatus()
method.
ConvertsToResource
You can provide any object with a toResource
method with a simple trait and attribute combination:
Testing
Testing endpoints that respond with enhanced resources is recommended to be done using Laravel's existing response assertions. One approach to creating resource asserter objects to help simplify the process that leverages functionality provided by enhanced resources can be found here.