class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRoutable (View source)

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
mixed $resource

The resource instance.

array $with

The additional data that should be added to the top-level resource array.

array $additional

The additional meta data that should be added to the resource response.

static string|null $wrap

The "data" wrapper that should be applied.

Methods

array
filter(array $data)

Filter the given data, removing any optional values.

array
mergeData(array $data, int $index, array $merge, bool $numericKeys)

Merge the given data in at the given index.

array
removeMissingValues(array $data)

Remove the missing values from the filtered data.

MissingValue|mixed
when(bool $condition, mixed $value, mixed $default = null)

Retrieve a value if the given "condition" is truthy.

MissingValue|mixed
unless(bool $condition, mixed $value, mixed $default = null)

Retrieve a value if the given "condition" is falsy.

MergeValue|mixed
merge(mixed $value)

Merge a value into the array.

MergeValue|mixed
mergeWhen(bool $condition, mixed $value, mixed $default = null)

Merge a value if the given condition is truthy.

MergeValue|mixed
mergeUnless(bool $condition, mixed $value, mixed $default = null)

Merge a value unless the given condition is truthy.

attributes(array $attributes)

Merge the given attributes.

MissingValue|mixed
whenHas(string $attribute, mixed $value = null, mixed $default = null)

Retrieve an attribute if it exists on the resource.

MissingValue|mixed
whenNull(mixed $value, mixed $default = null)

Retrieve a model attribute if it is null.

MissingValue|mixed
whenNotNull(mixed $value, mixed $default = null)

Retrieve a model attribute if it is not null.

MissingValue|mixed
whenAppended(string $attribute, mixed $value = null, mixed $default = null)

Retrieve an accessor when it has been appended.

MissingValue|mixed
whenLoaded(string $relationship, mixed $value = null, mixed $default = null)

Retrieve a relationship if it has been loaded.

MissingValue|mixed
whenCounted(string $relationship, mixed $value = null, mixed $default = null)

Retrieve a relationship count if it exists.

MissingValue|mixed
whenAggregated(string $relationship, string $column, string $aggregate, mixed $value = null, mixed $default = null)

Retrieve a relationship aggregated value if it exists.

MissingValue|mixed
whenExistsLoaded(string $relationship, mixed $value = null, mixed $default = null)

Retrieve a relationship existence check if it exists.

MissingValue|mixed
whenPivotLoaded(string $table, mixed $value, mixed $default = null)

Execute a callback if the given pivot table has been loaded.

MissingValue|mixed
whenPivotLoadedAs(string $accessor, string $table, mixed $value, mixed $default = null)

Execute a callback if the given pivot table with a custom accessor has been loaded.

bool
hasPivotLoaded(string $table)

Determine if the resource has the specified pivot table loaded.

bool
hasPivotLoadedAs(string $accessor, string $table)

Determine if the resource has the specified pivot table loaded with a custom accessor.

mixed
transform(mixed $value, callable $callback, mixed $default = null)

Transform the given value if it is present.

mixed
forwardCallTo(mixed $object, string $method, array $parameters)

Forward a method call to the given object.

mixed
forwardDecoratedCallTo(mixed $object, string $method, array $parameters)

Forward a method call to the given object, returning $this if the forwarded call returned itself.

static void
throwBadMethodCallException(string $method)

Throw a bad method call exception for the given method.

static void
macro(string $name, object|callable $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Checks if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
getRouteKey()

Get the value of the resource's route key.

string
getRouteKeyName()

Get the route key for the resource.

void
resolveRouteBinding(mixed $value, string|null $field = null)

Retrieve the model for a bound value.

void
resolveChildRouteBinding(string $childType, mixed $value, string|null $field = null)

Retrieve the model for a bound value.

bool
offsetExists(mixed $offset)

Determine if the given attribute exists.

mixed
offsetGet(mixed $offset)

Get the value for a given offset.

void
offsetSet(mixed $offset, mixed $value)

Set the value for a given offset.

void
offsetUnset(mixed $offset)

Unset the value for a given offset.

bool
__isset(string $key)

Determine if an attribute exists on the resource.

void
__unset(string $key)

Unset an attribute on the resource.

mixed
__get(string $key)

Dynamically get properties from the underlying resource.

void
__construct(mixed $resource)

Create a new resource instance.

static JsonResource
make(mixed ...$parameters)

Create a new resource instance.

collection(mixed $resource)

Create a new anonymous resource collection.

newCollection(mixed $resource)

Create a new resource collection instance.

array
resolve(Request|null $request = null)

Resolve the resource to an array.

toArray(Request $request)

Transform the resource into an array.

string
toJson(int $options = 0)

Convert the model instance to JSON.

array
with(Request $request)

Get any additional data that should be returned with the resource array.

$this
additional(array $data)

Add additional meta data to the resource response.

int
jsonOptions()

Get the JSON serialization options that should be applied to the resource response.

void
withResponse(Request $request, JsonResponse $response)

Customize the response for a request.

static void
wrap(string $value)

Set the string that should wrap the outer-most resource array.

static void
withoutWrapping()

Disable wrapping of the outer-most resource array.

response(Request|null $request = null)

Transform the resource into an HTTP response.

Response
toResponse(Request $request)

Create an HTTP response that represents the object.

array
jsonSerialize()

Prepare the resource for JSON serialization.

Details

protected array filter(array $data)

Filter the given data, removing any optional values.

Parameters

array $data

Return Value

array

protected array mergeData(array $data, int $index, array $merge, bool $numericKeys)

Merge the given data in at the given index.

Parameters

array $data
int $index
array $merge
bool $numericKeys

Return Value

array

protected array removeMissingValues(array $data)

Remove the missing values from the filtered data.

Parameters

array $data

Return Value

array

protected MissingValue|mixed when(bool $condition, mixed $value, mixed $default = null)

Retrieve a value if the given "condition" is truthy.

Parameters

bool $condition
mixed $value
mixed $default

Return Value

MissingValue|mixed

MissingValue|mixed unless(bool $condition, mixed $value, mixed $default = null)

Retrieve a value if the given "condition" is falsy.

Parameters

bool $condition
mixed $value
mixed $default

Return Value

MissingValue|mixed

protected MergeValue|mixed merge(mixed $value)

Merge a value into the array.

Parameters

mixed $value

Return Value

MergeValue|mixed

protected MergeValue|mixed mergeWhen(bool $condition, mixed $value, mixed $default = null)

Merge a value if the given condition is truthy.

Parameters

bool $condition
mixed $value
mixed $default

Return Value

MergeValue|mixed

protected MergeValue|mixed mergeUnless(bool $condition, mixed $value, mixed $default = null)

Merge a value unless the given condition is truthy.

Parameters

bool $condition
mixed $value
mixed $default

Return Value

MergeValue|mixed

protected MergeValue attributes(array $attributes)

Merge the given attributes.

Parameters

array $attributes

Return Value

MergeValue

MissingValue|mixed whenHas(string $attribute, mixed $value = null, mixed $default = null)

Retrieve an attribute if it exists on the resource.

Parameters

string $attribute
mixed $value
mixed $default

Return Value

MissingValue|mixed

protected MissingValue|mixed whenNull(mixed $value, mixed $default = null)

Retrieve a model attribute if it is null.

Parameters

mixed $value
mixed $default

Return Value

MissingValue|mixed

protected MissingValue|mixed whenNotNull(mixed $value, mixed $default = null)

Retrieve a model attribute if it is not null.

Parameters

mixed $value
mixed $default

Return Value

MissingValue|mixed

protected MissingValue|mixed whenAppended(string $attribute, mixed $value = null, mixed $default = null)

Retrieve an accessor when it has been appended.

Parameters

string $attribute
mixed $value
mixed $default

Return Value

MissingValue|mixed

protected MissingValue|mixed whenLoaded(string $relationship, mixed $value = null, mixed $default = null)

Retrieve a relationship if it has been loaded.

Parameters

string $relationship
mixed $value
mixed $default

Return Value

MissingValue|mixed

MissingValue|mixed whenCounted(string $relationship, mixed $value = null, mixed $default = null)

Retrieve a relationship count if it exists.

Parameters

string $relationship
mixed $value
mixed $default

Return Value

MissingValue|mixed

MissingValue|mixed whenAggregated(string $relationship, string $column, string $aggregate, mixed $value = null, mixed $default = null)

Retrieve a relationship aggregated value if it exists.

Parameters

string $relationship
string $column
string $aggregate
mixed $value
mixed $default

Return Value

MissingValue|mixed

MissingValue|mixed whenExistsLoaded(string $relationship, mixed $value = null, mixed $default = null)

Retrieve a relationship existence check if it exists.

Parameters

string $relationship
mixed $value
mixed $default

Return Value

MissingValue|mixed

protected MissingValue|mixed whenPivotLoaded(string $table, mixed $value, mixed $default = null)

Execute a callback if the given pivot table has been loaded.

Parameters

string $table
mixed $value
mixed $default

Return Value

MissingValue|mixed

protected MissingValue|mixed whenPivotLoadedAs(string $accessor, string $table, mixed $value, mixed $default = null)

Execute a callback if the given pivot table with a custom accessor has been loaded.

Parameters

string $accessor
string $table
mixed $value
mixed $default

Return Value

MissingValue|mixed

protected bool hasPivotLoaded(string $table)

Determine if the resource has the specified pivot table loaded.

Parameters

string $table

Return Value

bool

protected bool hasPivotLoadedAs(string $accessor, string $table)

Determine if the resource has the specified pivot table loaded with a custom accessor.

Parameters

string $accessor
string $table

Return Value

bool

protected mixed transform(mixed $value, callable $callback, mixed $default = null)

Transform the given value if it is present.

Parameters

mixed $value
callable $callback
mixed $default

Return Value

mixed

protected mixed forwardCallTo(mixed $object, string $method, array $parameters)

Forward a method call to the given object.

Parameters

mixed $object
string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

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.

Parameters

mixed $object
string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

static protected void throwBadMethodCallException(string $method)

Throw a bad method call exception for the given method.

Parameters

string $method

Return Value

void

Exceptions

BadMethodCallException

static void macro(string $name, object|callable $macro)

Register a custom macro.

Parameters

string $name
object|callable $macro

Return Value

void

static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

static bool hasMacro(string $name)

Checks if macro is registered.

Parameters

string $name

Return Value

bool

static void flushMacros()

Flush the existing macros.

Return Value

void

static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

mixed getRouteKey()

Get the value of the resource's route key.

Return Value

mixed

string getRouteKeyName()

Get the route key for the resource.

Return Value

string

void resolveRouteBinding(mixed $value, string|null $field = null)

Retrieve the model for a bound value.

Parameters

mixed $value
string|null $field

Return Value

void

Exceptions

Exception

void resolveChildRouteBinding(string $childType, mixed $value, string|null $field = null)

Retrieve the model for a bound value.

Parameters

string $childType
mixed $value
string|null $field

Return Value

void

Exceptions

Exception

bool offsetExists(mixed $offset)

Determine if the given attribute exists.

Parameters

mixed $offset

Return Value

bool

mixed offsetGet(mixed $offset)

Get the value for a given offset.

Parameters

mixed $offset

Return Value

mixed

void offsetSet(mixed $offset, mixed $value)

Set the value for a given offset.

Parameters

mixed $offset
mixed $value

Return Value

void

void offsetUnset(mixed $offset)

Unset the value for a given offset.

Parameters

mixed $offset

Return Value

void

bool __isset(string $key)

Determine if an attribute exists on the resource.

Parameters

string $key

Return Value

bool

void __unset(string $key)

Unset an attribute on the resource.

Parameters

string $key

Return Value

void

mixed __get(string $key)

Dynamically get properties from the underlying resource.

Parameters

string $key

Return Value

mixed

void __construct(mixed $resource)

Create a new resource instance.

Parameters

mixed $resource

Return Value

void

static JsonResource make(mixed ...$parameters)

Create a new resource instance.

Parameters

mixed ...$parameters

Return Value

JsonResource

static AnonymousResourceCollection collection(mixed $resource)

Create a new anonymous resource collection.

Parameters

mixed $resource

Return Value

AnonymousResourceCollection

static protected AnonymousResourceCollection newCollection(mixed $resource)

Create a new resource collection instance.

Parameters

mixed $resource

Return Value

AnonymousResourceCollection

array resolve(Request|null $request = null)

Resolve the resource to an array.

Parameters

Request|null $request

Return Value

array

array|Arrayable|JsonSerializable toArray(Request $request)

Transform the resource into an array.

Parameters

Request $request

Return Value

array|Arrayable|JsonSerializable

string toJson(int $options = 0)

Convert the model instance to JSON.

Parameters

int $options

Return Value

string

Exceptions

JsonEncodingException

array with(Request $request)

Get any additional data that should be returned with the resource array.

Parameters

Request $request

Return Value

array

$this additional(array $data)

Add additional meta data to the resource response.

Parameters

array $data

Return Value

$this

int jsonOptions()

Get the JSON serialization options that should be applied to the resource response.

Return Value

int

void withResponse(Request $request, JsonResponse $response)

Customize the response for a request.

Parameters

Request $request
JsonResponse $response

Return Value

void

static void wrap(string $value)

Set the string that should wrap the outer-most resource array.

Parameters

string $value

Return Value

void

static void withoutWrapping()

Disable wrapping of the outer-most resource array.

Return Value

void

JsonResponse response(Request|null $request = null)

Transform the resource into an HTTP response.

Parameters

Request|null $request

Return Value

JsonResponse

Response toResponse(Request $request)

Create an HTTP response that represents the object.

Parameters

Request $request

Return Value

Response

array jsonSerialize()

Prepare the resource for JSON serialization.

Return Value

array