Laravel - Serialization - Appending Values To Json
Occasionally, when converting models to arrays or JSON, you may wish to add attributes that do not have a corresponding column in your database. To do so, first define an accessor for the value:
attributes['admin'] === 'yes';
}
}
After creating the accessor, add the attribute name to the appends
property of your model. Note that attribute names are typically referenced using their "snake case" serialized representation, even though the accessor's PHP method is defined using "camel case":
Once the attribute has been added to the appends
list, it will be included in both the model's array and JSON representations. Attributes in the appends
array will also respect the visible
and hidden
settings configured on the model.