class Transformer

Responsible for applying transformations to Eloquent Models in order to customize their representation at the API.

Properties

protected array $aliases Attributes or relations that shall appear under a different key in the output.
protected array $drop_attributes Name the attributes that shall be omitted from the output.
protected array $drop_relations Name the relations that shall be omitted from the output.
protected ModelMapping $model_mapping Stores an instance of a model_mapping to retrieve transformers for related models.
protected Model $model Holds the eloquent instance to be transformed.
protected Array $output Holds the array form of the model.

Methods

__construct( ModelMapping $model_mapping)

No description

array
transform( Model $model)

Transform a single eloquent record.

void
handleRelations()

Add the relations to the output.

addRelation( String $name, Model|Collection $relation)

Add a single relation.

Array
transformAny( Collection|Model $thing)

Transform either Collection or Model using the known $model_mapping.

dropAttributes()

No description

void
formatAttributes()

Format all attributes if a function exists that matches their name.

void
alias()

Apply all rules in $this->aliases and replace key accordingly in output.

void
beforeSerialize()

Here you can hook in to change the output before serializing.

Array
serialize()

Serialize a single record to an array.

Details

at line line 58
__construct( ModelMapping $model_mapping)

Parameters

ModelMapping $model_mapping

at line line 69
array transform( Model $model)

Transform a single eloquent record.

Parameters

Model $model The eloquent model record to transform

Return Value

array

at line line 90
protected void handleRelations()

Add the relations to the output.

Return Value

void

at line line 106
protected addRelation( String $name, Model|Collection $relation)

Add a single relation.

Parameters

String $name
Model|Collection $relation

at line line 117
protected Array transformAny( Collection|Model $thing)

Transform either Collection or Model using the known $model_mapping.

Parameters

Collection|Model $thing to transform

Return Value

Array

at line line 132
protected dropAttributes()

at line line 146
protected void formatAttributes()

Format all attributes if a function exists that matches their name.

If you want to transform the attribute post_type, just define a formatPostType method that takes the old value of the attribute and returns a new one.

Return Value

void

at line line 162
protected void alias()

Apply all rules in $this->aliases and replace key accordingly in output.

Return Value

void

at line line 177
protected void beforeSerialize()

Here you can hook in to change the output before serializing.

Return Value

void

at line line 184
protected Array serialize()

Serialize a single record to an array.

Return Value

Array