Skip to main content

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 the toArray 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.
Use attributesToArray to convert only the model’s attributes to an array, without its relationships.
To convert an entire collection of models to an array, call toArray on the collection instance.

Serializing to JSON

Use the toJson 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.
You can also cast a model or collection to a string, which automatically calls toJson.
Because models and collections are converted to JSON when cast to a string, you can return Eloquent objects directly from your application’s routes or controllers. Laravel automatically serializes Eloquent models and collections returned from routes or controllers to JSON.

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 the Hidden 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 the Visible 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 the makeVisible or mergeVisible methods. makeVisible returns the model instance.
Conversely, use makeHidden or mergeHidden to hide attributes that are normally visible.
To fully override all visible or hidden attributes at once, use 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.
If you want an accessor to always be added to a model’s array and JSON representation, use the Appends attribute on the model. Even though accessor PHP methods are defined in “camelCase”, attribute names typically use the “snake_case” serialized form.
Once an attribute is added to the 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 the append 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.
Use 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 the serializeDate method. This method does not affect the format used when writing dates to the database.

Customizing the date format per attribute

You can customize the serialization format of individual Eloquent date attributes by specifying the date format in the model’s cast declaration.
Last modified on August 2, 2026