Skip to content

Commit

Permalink
document enum casting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 16, 2021
1 parent d1674f0 commit eb2f7fa
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions eloquent-mutators.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Attribute Casting](#attribute-casting)
- [Array & JSON Casting](#array-and-json-casting)
- [Date Casting](#date-casting)
- [Enum Casting](#enum-casting)
- [Encrypted Casting](#encrypted-casting)
- [Query Time Casting](#query-time-casting)
- [Custom Casts](#custom-casts)
Expand Down Expand Up @@ -326,6 +327,32 @@ By default, the `date` and `datetime` casts will serialize dates to a UTC ISO-86

If a custom format is applied to the `date` or `datetime` cast, such as `datetime:Y-m-d H:i:s`, the inner timezone of the Carbon instance will be used during date serialization. Typically, this will be the timezone specified in your application's `timezone` configuration option.

<a name="enum-casting"></a>
### Enum Casting

> {note} Enum casting is only available for PHP 8.1+.
Eloquent also allows you to cast your attribute values to PHP enums. To accomplish this, you may specify the attribute and enum you wish to cast in your model's `$casts` property array:

use App\Enums\ServerStatus;

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'status' => ServerStatus::class,
];

Once you have defined the cast on your model, the specified attribute will be automatically cast to and from an enum when you interact with the attribute:

if ($server->status == ServerStatus::provisioned) {
$server->status = ServerStatus::ready;

$server->save();
}

<a name="encrypted-casting"></a>
### Encrypted Casting

Expand Down

0 comments on commit eb2f7fa

Please sign in to comment.