Eloquent: Serialization

Eloquent: Serialization

Introduction

When building JSON APIs, 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 your serializations.

Basic Usage

Converting A Model To An Array

To convert a model and its loaded relationships to an array, you may use the toArray method. This method is recursive, so all attributes and all relations (including the relations of relations) will be converted to arrays: