Eloquent: Serialization
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 the toArray
method. This method is recursive, so all attributes and all relations (including the relations of relations) will be converted to arrays:
use App\Models\User;
$user = User::with('roles')->first();
return $user->toArray();
The attributesToArray
method may be used to convert a model's attributes to an array but not its relationships:
$user = User::first();
return $user->attributesToArray();
You may also convert entire collections of models to arrays by calling the toArray
method on the collection instance:
$users = User::all();
return $users->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 any JSON encoding options that are supported by PHP:
use App\Models\User;
$user = User::find(1);
return $user->toJson();
return $user->toJson(JSON_PRETTY_PRINT);
Alternatively, you may cast a model or collection to a string, which will automatically call the toJson
method on the model or collection:
return (string) User::find(1);
Since 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 will automatically serialize your Eloquent models and collections to JSON when they are returned from routes or controllers:
Route::get('/users', function () {
return User::all();
});
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, add a $hidden
property to your model. Attributes that are listed in the $hidden
property's array will not be included in the serialized representation of your model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['password'];
}
To hide relationships, add the relationship's method name to your Eloquent model's $hidden
property.
Alternatively, you may use the visible
property 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
array will be hidden when the model is converted to an array or JSON:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be visible in arrays.
*
* @var array
*/
protected $visible = ['first_name', 'last_name'];
}