JsonApiResource
class JsonApiResource extends JsonResource
Traits
Properties
| static protected array<class-string, bool> | $cachedPreserveKeysAttributes | The cached preserve keys attribute values. |
from ConditionallyLoadsAttributes |
| static protected array | $macros | The registered string macros. |
from Macroable |
| mixed | $resource | The resource instance. |
from JsonResource |
| array | $with | The additional data that should be added to the top-level resource array. |
from JsonResource |
| array | $additional | The additional metadata that should be added to the resource response. |
from JsonResource |
| static string|null | $wrap | The "data" wrapper that should be applied. |
|
| static bool | $forceWrapping | Whether to force wrapping even if the $wrap key exists in underlying resource data. |
from JsonResource |
| protected bool | $usesRequestQueryString | Determine whether resources respect sparse fieldsets from the request. |
from ResolvesJsonApiElements |
| protected bool | $includesPreviouslyLoadedRelationships | Determine whether included relationship for the resource from eager loaded relationship. |
from ResolvesJsonApiElements |
| protected null|array<int, string> | $requestedRelationships | The requested relationships for the resource. |
from ResolvesJsonApiElements |
| null|JsonApiResource, 1: string, 2: string, 3: bool}> | $loadedRelationshipsMap | Cached loaded relationships map. |
from ResolvesJsonApiElements |
| protected array | $loadedRelationshipIdentifiers | Cached loaded relationship identifiers. |
from ResolvesJsonApiElements |
| static int | $maxRelationshipDepth | The maximum relationship depth. |
from ResolvesJsonApiElements |
| static array{version?: string, ext?: array, profile?: array, meta?: array} | $jsonApiInformation | The resource's "version" for JSON:API. |
|
| protected array | $jsonApiLinks | The resource's "links" for JSON:API. |
|
| protected array | $jsonApiMeta | The resource's "meta" for JSON:API. |
|
| protected array|null | $includedResources | The included resources resolved for this response. |
Methods
Filter the given data, removing any optional values.
Merge the given data in at the given index.
Remove the missing values from the filtered data.
Retrieve a value if the given "condition" is truthy.
Retrieve a value if the given "condition" is falsy.
Merge a value if the given condition is truthy.
Merge a value unless the given condition is truthy.
Merge the given attributes.
Retrieve an attribute if it exists on the resource.
Retrieve a model attribute if it is null.
Retrieve a model attribute if it is not null.
Retrieve an accessor when it has been appended.
Retrieve a relationship if it has been loaded.
Retrieve a relationship count if it exists.
Retrieve a relationship aggregated value if it exists.
Retrieve a relationship existence check if it exists.
Execute a callback if the given pivot table has been loaded.
Execute a callback if the given pivot table with a custom accessor has been loaded.
Determine if the resource has the specified pivot table loaded.
Determine if the resource has the specified pivot table loaded with a custom accessor.
Transform the given value if it is present.
Forward a method call to the given object.
Forward a method call to the given object, returning $this if the forwarded call returned itself.
Throw a bad method call exception for the given method.
Mix another object into the class.
Dynamically handle calls to the class.
Dynamically handle calls to the class.
Retrieve the model for a bound value.
Retrieve the model for a bound value.
Set the value for a given offset.
Determine if an attribute exists on the resource.
Dynamically get properties from the underlying resource.
Create a new anonymous resource collection.
Create a new resource collection instance.
Convert the resource to pretty print formatted JSON.
Add additional metadata to the resource response.
Get the JSON serialization options that should be applied to the resource response.
Customize the outgoing response for the resource.
Resolve the HTTP request instance from container.
Set the string that should wrap the outer-most resource array.
Disable wrapping of the outer-most resource array.
Flush all static state.
Specify the maximum relationship depth.
Resolve the resource's attributes.
Resolve relationships for the resource's data object.
Compile resource relations.
Get the requested relationships for this resource or one of its relationships.
Get the declared resolvers for the requested resource relationships.
Load requested relationships in batches before serializing their attributes.
Resolve included resources and preserve linkage when combining duplicate identities.
Merge relationship linkage without discarding descendants of duplicate resources.
Resolve the meta information for the resource.
Indicate that attributes should respect the request's sparse fieldsets.
Indicate that attributes should ignore the request's sparse fieldsets.
Indicate that relationship should include loaded relationships.
Resolve a JSON API request instance from the given HTTP request.
Set the JSON:API version for the request.
Details
in
ConditionallyLoadsAttributes at line 24
protected array
filter(array $data)
Filter the given data, removing any optional values.
in
ConditionallyLoadsAttributes at line 57
protected array
mergeData(array $data, int $index, array $merge, bool $numericKeys)
Merge the given data in at the given index.
in
ConditionallyLoadsAttributes at line 74
protected array
removeMissingValues(array $data)
Remove the missing values from the filtered data.
in
ConditionallyLoadsAttributes at line 109
protected mixed
when(bool $condition, mixed $value, mixed $default = new MissingValue())
Retrieve a value if the given "condition" is truthy.
in
ConditionallyLoadsAttributes at line 123
mixed
unless(bool $condition, mixed $value, mixed $default = new MissingValue())
Retrieve a value if the given "condition" is falsy.
in
ConditionallyLoadsAttributes at line 135
protected mixed
merge(mixed $value)
Merge a value into the array.
in
ConditionallyLoadsAttributes at line 145
protected mixed
mergeWhen(bool $condition, mixed $value, mixed $default = new MissingValue())
Merge a value if the given condition is truthy.
in
ConditionallyLoadsAttributes at line 159
protected mixed
mergeUnless(bool $condition, mixed $value, mixed $default = new MissingValue())
Merge a value unless the given condition is truthy.
in
ConditionallyLoadsAttributes at line 169
protected MergeValue
attributes(array $attributes)
Merge the given attributes.
in
ConditionallyLoadsAttributes at line 181
mixed
whenHas(string $attribute, mixed $value = null, mixed $default = new MissingValue())
Retrieve an attribute if it exists on the resource.
in
ConditionallyLoadsAttributes at line 197
protected mixed
whenNull(mixed $value, mixed $default = new MissingValue())
Retrieve a model attribute if it is null.
in
ConditionallyLoadsAttributes at line 209
protected mixed
whenNotNull(mixed $value, mixed $default = new MissingValue())
Retrieve a model attribute if it is not null.
in
ConditionallyLoadsAttributes at line 221
protected mixed
whenAppended(string $attribute, mixed $value = null, mixed $default = new MissingValue())
Retrieve an accessor when it has been appended.
in
ConditionallyLoadsAttributes at line 235
protected mixed
whenLoaded(string $relationship, mixed $value = null, mixed $default = new MissingValue())
Retrieve a relationship if it has been loaded.
in
ConditionallyLoadsAttributes at line 263
mixed
whenCounted(string $relationship, mixed $value = null, mixed $default = new MissingValue())
Retrieve a relationship count if it exists.
in
ConditionallyLoadsAttributes at line 291
mixed
whenAggregated(string $relationship, string $column, string $aggregate, mixed $value = null, mixed $default = new MissingValue())
Retrieve a relationship aggregated value if it exists.
in
ConditionallyLoadsAttributes at line 319
mixed
whenExistsLoaded(string $relationship, mixed $value = null, mixed $default = new MissingValue())
Retrieve a relationship existence check if it exists.
in
ConditionallyLoadsAttributes at line 343
protected mixed
whenPivotLoaded(string $table, mixed $value, mixed $default = new MissingValue())
Execute a callback if the given pivot table has been loaded.
in
ConditionallyLoadsAttributes at line 353
protected mixed
whenPivotLoadedAs(string $accessor, string $table, mixed $value, mixed $default = new MissingValue())
Execute a callback if the given pivot table with a custom accessor has been loaded.
in
ConditionallyLoadsAttributes at line 365
protected bool
hasPivotLoaded(string $table)
Determine if the resource has the specified pivot table loaded.
in
ConditionallyLoadsAttributes at line 373
protected bool
hasPivotLoadedAs(string $accessor, string $table)
Determine if the resource has the specified pivot table loaded with a custom accessor.
in
ConditionallyLoadsAttributes at line 383
protected mixed
transform(mixed $value, callable $callback, mixed $default = new MissingValue())
Transform the given value if it is present.
in
ForwardsCalls at line 22
protected mixed
forwardCallTo(mixed $object, string $method, array $parameters)
Forward a method call to the given object.
in
ForwardsCalls at line 52
protected mixed
forwardDecoratedCallTo(mixed $object, string $method, array $parameters)
Forward a method call to the given object, returning $this if the forwarded call returned itself.
in
ForwardsCalls at line 66
static protected never
throwBadMethodCallException(string $method)
Throw a bad method call exception for the given method.
in
Macroable at line 28
static void
macro(string $name, callable|object $macro)
Register a custom macro.
Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.
in
Macroable at line 41
static void
mixin(object $mixin, bool $replace = true)
Mix another object into the class.
Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.
in
Macroable at line 57
static bool
hasMacro(string $name)
Check if macro is registered.
in
Macroable at line 68
static void
flushMacros()
Flush the existing macros.
Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.
in
Macroable at line 78
static mixed
__callStatic(string $method, array $parameters)
Dynamically handle calls to the class.
in
Macroable at line 103
mixed
__call(string $method, array $parameters)
Dynamically handle calls to the class.
in
DelegatesToResource at line 21
mixed
getRouteKey()
Get the value of the resource's route key.
in
DelegatesToResource at line 29
string
getRouteKeyName()
Get the route key for the resource.
in
DelegatesToResource at line 39
never
resolveRouteBinding(mixed $value, string|null $field = null)
Retrieve the model for a bound value.
in
DelegatesToResource at line 49
Model|null
resolveChildRouteBinding(string $childType, mixed $value, string|null $field = null)
Retrieve the model for a bound value.
in
DelegatesToResource at line 57
bool
offsetExists(mixed $offset)
Determine if the given attribute exists.
in
DelegatesToResource at line 65
mixed
offsetGet(mixed $offset)
Get the value for a given offset.
in
DelegatesToResource at line 73
void
offsetSet(mixed $offset, mixed $value)
Set the value for a given offset.
in
DelegatesToResource at line 81
void
offsetUnset(mixed $offset)
Unset the value for a given offset.
in
DelegatesToResource at line 89
bool
__isset(string $key)
Determine if an attribute exists on the resource.
in
DelegatesToResource at line 97
void
__unset(string $key)
Unset an attribute on the resource.
in
DelegatesToResource at line 105
mixed
__get(string $key)
Dynamically get properties from the underlying resource.
in
JsonResource at line 57
__construct(mixed $resource)
Create a new resource instance.
in
JsonResource at line 65
static JsonResource
make(mixed ...$parameters)
Create a new resource instance.
in
JsonResource at line 73
static AnonymousResourceCollection
collection(mixed $resource)
Create a new anonymous resource collection.
at line 216
static protected AnonymousResourceCollection
newCollection(mixed $resource)
Create a new resource collection instance.
at line 164
array
resolve(Request|null $request = null)
Resolve the resource to an array.
at line 84
array|Arrayable|JsonSerializable
toAttributes(Request $request)
Transform the resource into an array.
at line 175
array|Arrayable|JsonSerializable
resolveResourceData(Request $request)
Resolve the resource data to an array.
at line 97
array|Arrayable|JsonSerializable
toArray(Request $request)
Transform the resource into its default attributes.
in
JsonResource at line 153
string
toJson(int $options = 0)
Convert the resource to JSON.
in
JsonResource at line 169
string
toPrettyJson(int $options = 0)
Convert the resource to pretty print formatted JSON.
at line 146
array
with(Request $request)
Get any additional data that should be returned with the resource array.
in
JsonResource at line 185
JsonResource
additional(array $data)
Add additional metadata to the resource response.
in
JsonResource at line 195
int
jsonOptions()
Get the JSON serialization options that should be applied to the resource response.
at line 184
void
withResponse(Request $request, JsonResponse $response)
Customize the outgoing response for the resource.
at line 207
protected Request
resolveRequestFromContainer()
Resolve the HTTP request instance from container.
at line 227
static void
wrap(string $value)
Set the string that should wrap the outer-most resource array.
at line 238
static void
withoutWrapping()
Disable wrapping of the outer-most resource array.
in
JsonResource at line 242
JsonResponse
response(Request|null $request = null)
Transform the resource into an HTTP response.
at line 193
Response
toResponse(Request $request)
Create an HTTP response that represents the object.
in
JsonResource at line 260
array
jsonSerialize()
Prepare the resource for JSON serialization.
at line 247
static void
flushState()
Flush all static state.
in
ResolvesJsonApiElements at line 77
static void
maxRelationshipDepth(int $depth)
Specify the maximum relationship depth.
Boot-only. The depth limit persists in a static property for the worker lifetime and affects every subsequent JSON:API resource resolution.
in
ResolvesJsonApiElements at line 85
protected array
resolveResourceObject(JsonApiRequest $request)
Resolve data for the resource.
in
ResolvesJsonApiElements at line 108
string
resolveResourceIdentifier(JsonApiRequest $request)
Resolve the resource's identifier.
in
ResolvesJsonApiElements at line 132
string
resolveResourceType(JsonApiRequest $request)
Resolve the resource's type.
in
ResolvesJsonApiElements at line 160
protected array
resolveResourceAttributes(JsonApiRequest $request, string $resourceType)
Resolve the resource's attributes.
in
ResolvesJsonApiElements at line 188
protected array
resolveResourceRelationshipIdentifiers(JsonApiRequest $request)
Resolve relationships for the resource's data object.
in
ResolvesJsonApiElements at line 207
protected void
compileResourceRelationships(JsonApiRequest $request)
Compile resource relationships.
in
ResolvesJsonApiElements at line 238
protected Generator
compileResourceRelationshipUsingResolver(JsonApiRequest $request, mixed $resource, RelationResolver $relationResolver, Collection|Model|null $relatedModels)
Compile resource relations.
in
ResolvesJsonApiElements at line 326
protected array
requestedResourceRelationships(JsonApiRequest $request, string|null $relationName = null)
Get the requested relationships for this resource or one of its relationships.
in
ResolvesJsonApiElements at line 359
protected Collection
getResourceRelationships(JsonApiRequest $request)
Get the declared resolvers for the requested resource relationships.
in
ResolvesJsonApiElements at line 373
static void
prepareResourceRelationships(Collection $resources, JsonApiRequest $request)
| internal |
Load requested relationships in batches before serializing their attributes.
in
ResolvesJsonApiElements at line 425
Collection
resolveIncludedResourceObjects(JsonApiRequest $request)
Resolve included for the resource.
in
ResolvesJsonApiElements at line 489
static array
resolveIncludedResources(Collection $resources, JsonApiRequest $request)
| internal |
Resolve included resources and preserve linkage when combining duplicate identities.
in
ResolvesJsonApiElements at line 543
static protected void
mergeResourceRelationships(array $relationships, array $additional, array $identifiers)
Merge relationship linkage without discarding descendants of duplicate resources.
in
ResolvesJsonApiElements at line 597
protected array
resolveResourceLinks(JsonApiRequest $request)
Resolve the links for the resource.
in
ResolvesJsonApiElements at line 607
protected array
resolveResourceMetaInformation(JsonApiRequest $request)
Resolve the meta information for the resource.
in
ResolvesJsonApiElements at line 615
ResolvesJsonApiElements
respectFieldsAndIncludesInQueryString(bool $value = true)
Indicate that attributes should respect the request's sparse fieldsets.
in
ResolvesJsonApiElements at line 625
ResolvesJsonApiElements
ignoreFieldsAndIncludesInQueryString()
Indicate that attributes should ignore the request's sparse fieldsets.
in
ResolvesJsonApiElements at line 633
ResolvesJsonApiElements
includePreviouslyLoadedRelationships()
Indicate that relationship should include loaded relationships.
in
ResolvesJsonApiRequest at line 15
protected JsonApiRequest
resolveJsonApiRequestFrom(Request $request)
Resolve a JSON API request instance from the given HTTP request.
at line 55
static void
configure(string|null $version = null, array $ext = [], array $profile = [], array $meta = [])
Set the JSON:API version for the request.
Boot-only. The configuration persists in a static property for the worker lifetime and applies to every JSON:API response across all coroutines.
at line 68
string|null
toId(Request $request)
Get the resource's ID.
at line 76
string|null
toType(Request $request)
Get the resource's type.
at line 130
array
toLinks(Request $request)
Get the resource's links.
at line 138
array
toMeta(Request $request)
Get the resource's meta information.