Documentation Index
Fetch the complete documentation index at: https://kawax.biz/llms.txt
Use this file to discover all available pages before exploring further.
Introduction
When building APIs using Laravel, you will often need to convert your models and relationships to arrays or JSON. Eloquent includes convenient methods for making these conversions, as well as controlling which attributes are included in the serialized representation of your models.For an even more robust way of handling Eloquent model and collection JSON serialization, check out the documentation on Eloquent API resources.
Serializing Models and Collections
Serializing to Arrays
To convert a model and its loaded relationships to an array, you should use thetoArray method. This method is recursive, so all attributes and all relations (including the relations of relations) will be converted to arrays:
attributesToArray method may be used to convert a model’s attributes to an array but not 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 relations 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 “camel case” method names, a relationship’s JSON attribute will be “snake case”.Hiding Attributes From JSON
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, you may 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
Alternatively, you may 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 methods. The makeVisible method returns the model instance:
makeHidden or mergeHidden methods:
setVisible and setHidden methods respectively:
Appending Computed Attributes
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 attribute names are typically referenced using their “snake case” serialized representation, even though the accessor’s PHP method is defined using “camel case”:
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 Run Time
At runtime, you may instruct a model instance to append additional attributes using theappend or mergeAppends methods. Or, you may 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: