Eloquent: Serialization

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

The attributesToArray method may be used to convert a model’s attributes to an array but not its relationships:

    You may also convert entire of models to arrays by calling the toArray method on the collection instance:

    1. $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 :

    1. 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:

    1. return (string) User::find(1);

    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”.

    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 property’s array will not be included in the serialized representation of your model:

    1. <?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'];}

    Note
    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:

    1. <?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'];}

    Temporarily Modifying Attribute Visibility

    If you would like to make some typically hidden attributes visible on a given model instance, you may use the makeVisible method. The makeVisible method returns the model instance:

    1. return $user->makeVisible('attribute')->toArray();

    Likewise, if you would like to hide some attributes that are typically visible, you may use the makeHidden method.

    1. return $user->makeHidden('attribute')->toArray();

    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:

    1. <?php namespace App\Models; use Illuminate\Database\Eloquent\Casts\Attribute;use Illuminate\Database\Eloquent\Model; class User extends Model{ /** * Determine if the user is an administrator. */ protected function isAdmin(): Attribute { return new Attribute( get: fn () => 'yes', ); }}

    If you would like the accessor to always be appended to your model’s array and JSON representations, you may add the attribute name to the appends property of 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”:

    1. <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model{ /** * The accessors to append to the model's array form. * * @var array */ protected $appends = ['is_admin'];}

    Once the attribute has been added to the 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 the append method. Or, you may use the setAppends method to override the entire array of appended properties for a given model instance:

      Customizing The Default Date Format

      You may customize the default serialization format by overriding the serializeDate method. This method does not affect how your dates are formatted for storage in the database:

      Customizing The Date Format Per Attribute

      You may customize the serialization format of individual Eloquent date attributes by specifying the date format in the model’s :