Introduction
When building APIs with Laravel, you’ll often need to convert your models and their relationships to arrays or JSON. Eloquent includes convenient methods for performing these conversions, as well as controlling which attributes are included in the serialized representation of your models.For an even more robust way to handle Eloquent model and collection JSON serialization, see the documentation on Eloquent API resources.
Models and arrays
Serializing to arrays
To convert a model and its loaded relationships to an array, use thetoArray method.
This method is recursive, so all attributes and all relationships (including the relationships’ relationships) will be converted to arrays.
attributesToArray method may be used to convert a model’s attributes to an array without its relationships.
toArray method on the collection instance.
Serializing to JSON
To convert a model to JSON, you should use thetoJson method.
Like toArray, the toJson method is recursive, so all attributes and relationships will be converted to JSON.
You may also specify any JSON encoding options that are supported by PHP.
toJson method on the model or collection.
Relationships
When an Eloquent model is converted to JSON, its loaded relationships will automatically be included as attributes on the JSON object. Also, though Eloquent relationship methods are defined using “camelCase” method names, a relationship’s JSON attribute will be “snake_case”.Controlling attribute visibility
Hiding attributes
Sometimes you may wish to limit the attributes, such as passwords, that are included in your model’s array or JSON representation. To do so, use theHidden attribute on your model.
Attributes that are listed in the Hidden attribute will not be included in the serialized representation of your model.
To hide relationships, add the relationship’s method name to your Eloquent model’s Hidden attribute.
Making attributes visible
You may also use theVisible attribute to define an “allow list” of attributes that should be included in your model’s array and JSON representation.
All attributes that are not present in the Visible attribute will be hidden when the model is converted to an array or JSON.
Temporarily modifying attribute visibility
If you would like to make some typically hidden attributes visible on a given model instance, you may use themakeVisible or mergeVisible method.
The makeVisible method returns the model instance.
makeHidden or mergeHidden method.
setVisible and setHidden methods respectively.
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.Appends attribute on your model.
Note that the accessor’s PHP method is defined using “camelCase”, while the attribute name is typically referenced using its “snake_case” serialized representation.
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.
Appending at runtime
At runtime, you may instruct a model instance to append additional attributes using theappend or mergeAppends method.
You can also use the setAppends method to override the entire array of appended properties for a given model instance.
withoutAppends method.
Date serialization
Customizing the default date format
You may customize the default serialization format by overriding theserializeDate method.
This method does not affect how your dates are formatted for storage in the database.