# Fluent ORM: Serialization
## Introduction
When building JSON APIs, you will often need to convert your models and relationships to arrays or JSON. Fluent ORM includes convenient methods for making these conversions, as well as controlling which attributes are included in your serializations.
## Serializing Models & Collections
### Serializing To Arrays
To convert a model and its loaded `relationships` to an array, you should use the `toArray` method. This method is recursive, so all attributes and all relations (including the relations of relations) will be converted to arrays:
```php
$user = FluentCrm\App\Models\User::with('roles')->first();
return $user->toArray();
```
You may also convert entire collections of models to arrays:
```php
$user = FluentCrm\App\Models\User::all();
return $user->toArray();
```
### Serializing To JSON
To convert a model to JSON, you should use the `toJson` method. Like `toArray`, the toJson method is recursive, so all attributes and relations will be converted to JSON. You may also specify JSON encoding options supported by PHP:
```php
$user = FluentCrm\App\Models\User::find(1);
return $user->toJson();
return $user->toJson(JSON_PRETTY_PRINT);
```
## 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, add a `$hidden` property to your model:
```php
makeVisible('attribute')->toArray();
```
Likewise, if you would like to make some typically visible attributes hidden on a given model instance, you may use the `makeHidden` method.
```php
return $user->makeHidden('attribute')->toArray();
```
## Appending Values To JSON
Occasionally, when casting models to an array 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:
```php
attributes['admin'] == 'yes';
}
}
```
After creating the accessor, add the attribute name to the `appends` property on the model. Note that attribute names are typically referenced in "snake case", even though the accessor is defined using "camel case":
```php
append('is_admin')->toArray();
return $user->setAppends(['is_admin'])->toArray();
```