Introduction
When you build an API with Laravel, you often need to convert models and their relationships into arrays or JSON. Eloquent includes convenient methods for these conversions and features that let you control which attributes appear in the serialized representation of a model.For a more robust way to serialize Eloquent models and collections to JSON, see the Eloquent API resources documentation.
Models and arrays
Serializing to an array
Use thetoArray method to convert a model and its loaded relationships to an array.
The method works recursively, so every attribute and every relationship (including nested relationships) is converted to an array.
attributesToArray to convert only the model’s attributes to an array, without its relationships.
toArray on the collection instance.
Serializing to JSON
Use thetoJson method to convert a model to JSON.
Like toArray, toJson is recursive, so every attribute and relationship is converted to JSON.
You can also pass any JSON encoding option supported by PHP.
toJson.
Relationships
When an Eloquent model is converted to JSON, its loaded relationships are automatically included as attributes on the JSON object. Also, while Eloquent relationship methods are defined with “camelCase” names, the JSON attributes for the relationships use “snake_case”.Controlling attribute visibility
Hiding attributes
You may want to limit attributes — such as passwords — from a model’s array or JSON representation. To do so, use theHidden attribute on the model.
Attributes listed on the Hidden attribute will not be included in the serialized representation of the model.
To hide a relationship, add its method name to the model’s Hidden attribute.
Exposing attributes
Alternatively, use theVisible attribute to define an “allow list” of attributes that should be included in the model’s array and JSON representation.
Every attribute not present in Visible is hidden when the model is converted to an array or JSON.
Temporarily showing or hiding attributes
To make normally hidden attributes visible on a specific model instance, use themakeVisible or mergeVisible methods.
makeVisible returns the model instance.
makeHidden or mergeHidden to hide attributes that are normally visible.
setVisible and setHidden respectively.
Appended attributes
When converting a model to an array or JSON, you may want to add attributes that don’t have a corresponding column in the database. To do so, first define an accessor for the value.Appends attribute on the model.
Even though accessor PHP methods are defined in “camelCase”, attribute names typically use the “snake_case” serialized form.
appends list, it’s included in both the model’s array and JSON representation.
Attributes in the appends array also respect the visible and hidden settings on the model.
Appending at runtime
At runtime, use theappend or mergeAppends methods to instruct a model instance to append additional attributes.
You can also use the setAppends method to override the entire array of appended properties on a specific model instance.
withoutAppends to remove all appended properties from a model.
Date serialization
Customizing the default date format
You can customize the default serialization format by overriding theserializeDate method.
This method does not affect the format used when writing dates to the database.