Skip to content

Commit 298272b

Browse files
authored
Documents casts method in favour of casts property (#9300)
1 parent 6ace3e8 commit 298272b

File tree

2 files changed

+112
-70
lines changed

2 files changed

+112
-70
lines changed

eloquent-mutators.md

Lines changed: 105 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ protected function address(): Attribute
196196
<a name="attribute-casting"></a>
197197
## Attribute Casting
198198

199-
Attribute casting provides functionality similar to accessors and mutators without requiring you to define any additional methods on your model. Instead, your model's `$casts` property provides a convenient method of converting attributes to common data types.
199+
Attribute casting provides functionality similar to accessors and mutators without requiring you to define any additional methods on your model. Instead, your model's `casts` method provides a convenient way of converting attributes to common data types.
200200

201-
The `$casts` property should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to. The supported cast types are:
201+
The `casts` method should return an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to. The supported cast types are:
202202

203203
<div class="content-list" markdown="1">
204204

@@ -237,13 +237,16 @@ To demonstrate attribute casting, let's cast the `is_admin` attribute, which is
237237
class User extends Model
238238
{
239239
/**
240-
* The attributes that should be cast.
240+
* Get the attributes that should be cast.
241241
*
242-
* @var array
242+
* @return array<string, string>
243243
*/
244-
protected $casts = [
245-
'is_admin' => 'boolean',
246-
];
244+
protected function casts(): array
245+
{
246+
return [
247+
'is_admin' => 'boolean',
248+
];
249+
}
247250
}
248251

249252
After defining the cast, the `is_admin` attribute will always be cast to a boolean when you access it, even if the underlying value is stored in the database as an integer:
@@ -279,13 +282,16 @@ You may use the `Illuminate\Database\Eloquent\Casts\AsStringable` cast class to
279282
class User extends Model
280283
{
281284
/**
282-
* The attributes that should be cast.
285+
* Get the attributes that should be cast.
283286
*
284-
* @var array
287+
* @return array<string, string>
285288
*/
286-
protected $casts = [
287-
'directory' => AsStringable::class,
288-
];
289+
protected function casts(): array
290+
{
291+
return [
292+
'directory' => AsStringable::class,
293+
];
294+
}
289295
}
290296

291297
<a name="array-and-json-casting"></a>
@@ -302,13 +308,16 @@ The `array` cast is particularly useful when working with columns that are store
302308
class User extends Model
303309
{
304310
/**
305-
* The attributes that should be cast.
311+
* Get the attributes that should be cast.
306312
*
307-
* @var array
313+
* @return array<string, string>
308314
*/
309-
protected $casts = [
310-
'options' => 'array',
311-
];
315+
protected function casts(): array
316+
{
317+
return [
318+
'options' => 'array',
319+
];
320+
}
312321
}
313322

314323
Once the cast is defined, you may access the `options` attribute and it will automatically be deserialized from JSON into a PHP array. When you set the value of the `options` attribute, the given array will automatically be serialized back into JSON for storage:
@@ -345,56 +354,68 @@ To solve this, Laravel offers an `AsArrayObject` cast that casts your JSON attri
345354
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
346355

347356
/**
348-
* The attributes that should be cast.
357+
* Get the attributes that should be cast.
349358
*
350-
* @var array
359+
* @return array<string, string>
351360
*/
352-
protected $casts = [
353-
'options' => AsArrayObject::class,
354-
];
361+
protected function casts(): array
362+
{
363+
return [
364+
'options' => AsArrayObject::class,
365+
];
366+
}
355367

356368
Similarly, Laravel offers an `AsCollection` cast that casts your JSON attribute to a Laravel [Collection](/docs/{{version}}/collections) instance:
357369

358370
use Illuminate\Database\Eloquent\Casts\AsCollection;
359371

360372
/**
361-
* The attributes that should be cast.
373+
* Get the attributes that should be cast.
362374
*
363-
* @var array
375+
* @return array<string, string>
364376
*/
365-
protected $casts = [
366-
'options' => AsCollection::class,
367-
];
377+
protected function casts(): array
378+
{
379+
return [
380+
'options' => AsCollection::class,
381+
];
382+
}
368383

369384
If you would like the `AsCollection` cast to instantiate a custom collection class instead of Laravel's base collection class, you may provide the collection class name as a cast argument:
370385

371386
use App\Collections\OptionCollection;
372387
use Illuminate\Database\Eloquent\Casts\AsCollection;
373388

374389
/**
375-
* The attributes that should be cast.
390+
* Get the attributes that should be cast.
376391
*
377-
* @var array
392+
* @return array<string, string>
378393
*/
379-
protected $casts = [
380-
'options' => AsCollection::class.':'.OptionCollection::class,
381-
];
394+
protected function casts(): array
395+
{
396+
return [
397+
'options' => AsCollection::using(OptionCollection::class),
398+
];
399+
}
382400

383401
<a name="date-casting"></a>
384402
### Date Casting
385403

386-
By default, Eloquent will cast the `created_at` and `updated_at` columns to instances of [Carbon](https://github.com/briannesbitt/Carbon), which extends the PHP `DateTime` class and provides an assortment of helpful methods. You may cast additional date attributes by defining additional date casts within your model's `$casts` property array. Typically, dates should be cast using the `datetime` or `immutable_datetime` cast types.
404+
By default, Eloquent will cast the `created_at` and `updated_at` columns to instances of [Carbon](https://github.com/briannesbitt/Carbon), which extends the PHP `DateTime` class and provides an assortment of helpful methods. You may cast additional date attributes by defining additional date casts within your model's `casts` method. Typically, dates should be cast using the `datetime` or `immutable_datetime` cast types.
387405

388406
When defining a `date` or `datetime` cast, you may also specify the date's format. This format will be used when the [model is serialized to an array or JSON](/docs/{{version}}/eloquent-serialization):
389407

390408
/**
391-
* The attributes that should be cast.
409+
* Get the attributes that should be cast.
392410
*
393-
* @var array
411+
* @return array<string, string>
394412
*/
395-
protected $casts = [
396-
'created_at' => 'datetime:Y-m-d',
397-
];
413+
protected function casts(): array
414+
{
415+
return [
416+
'created_at' => 'datetime:Y-m-d',
417+
];
418+
}
398419

399420
When a column is cast as a date, you may set the corresponding model attribute value to a UNIX timestamp, date string (`Y-m-d`), date-time string, or a `DateTime` / `Carbon` instance. The date's value will be correctly converted and stored in your database.
400421

@@ -427,18 +448,21 @@ If a custom format is applied to the `date` or `datetime` cast, such as `datetim
427448
<a name="enum-casting"></a>
428449
### Enum Casting
429450

430-
Eloquent also allows you to cast your attribute values to PHP [Enums](https://www.php.net/manual/en/language.enumerations.backed.php). To accomplish this, you may specify the attribute and enum you wish to cast in your model's `$casts` property array:
451+
Eloquent also allows you to cast your attribute values to PHP [Enums](https://www.php.net/manual/en/language.enumerations.backed.php). To accomplish this, you may specify the attribute and enum you wish to cast in your model's `casts` method:
431452

432453
use App\Enums\ServerStatus;
433454

434455
/**
435-
* The attributes that should be cast.
456+
* Get the attributes that should be cast.
436457
*
437-
* @var array
458+
* @return array<string, string>
438459
*/
439-
protected $casts = [
440-
'status' => ServerStatus::class,
441-
];
460+
protected function casts(): array
461+
{
462+
return [
463+
'status' => ServerStatus::class,
464+
];
465+
}
442466

443467
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:
444468

@@ -457,13 +481,16 @@ Sometimes you may need your model to store an array of enum values within a sing
457481
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
458482

459483
/**
460-
* The attributes that should be cast.
484+
* Get the attributes that should be cast.
461485
*
462-
* @var array
486+
* @return array<string, string>
463487
*/
464-
protected $casts = [
465-
'statuses' => AsEnumCollection::class.':'.ServerStatus::class,
466-
];
488+
protected function casts(): array
489+
{
490+
return [
491+
'statuses' => AsEnumCollection::of(ServerStatus::class),
492+
];
493+
}
467494

468495
<a name="encrypted-casting"></a>
469496
### Encrypted Casting
@@ -555,13 +582,16 @@ Once you have defined a custom cast type, you may attach it to a model attribute
555582
class User extends Model
556583
{
557584
/**
558-
* The attributes that should be cast.
585+
* Get the attributes that should be cast.
559586
*
560-
* @var array
587+
* @return array<string, string>
561588
*/
562-
protected $casts = [
563-
'options' => Json::class,
564-
];
589+
protected function casts(): array
590+
{
591+
return [
592+
'options' => Json::class,
593+
];
594+
}
565595
}
566596

567597
<a name="value-object-casting"></a>
@@ -708,13 +738,16 @@ A classic example of an inbound only cast is a "hashing" cast. For example, we m
708738
When attaching a custom cast to a model, cast parameters may be specified by separating them from the class name using a `:` character and comma-delimiting multiple parameters. The parameters will be passed to the constructor of the cast class:
709739

710740
/**
711-
* The attributes that should be cast.
741+
* Get the attributes that should be cast.
712742
*
713-
* @var array
743+
* @return array<string, string>
714744
*/
715-
protected $casts = [
716-
'secret' => Hash::class.':sha256',
717-
];
745+
protected function casts(): array
746+
{
747+
return [
748+
'secret' => Hash::class.':sha256',
749+
];
750+
}
718751

719752
<a name="castables"></a>
720753
### Castables
@@ -723,9 +756,12 @@ You may want to allow your application's value objects to define their own custo
723756

724757
use App\ValueObjects\Address;
725758

726-
protected $casts = [
727-
'address' => Address::class,
728-
];
759+
protected function casts(): array
760+
{
761+
return [
762+
'address' => Address::class,
763+
];
764+
}
729765

730766
Objects that implement the `Castable` interface must define a `castUsing` method that returns the class name of the custom caster class that is responsible for casting to and from the `Castable` class:
731767

@@ -749,13 +785,16 @@ Objects that implement the `Castable` interface must define a `castUsing` method
749785
}
750786
}
751787

752-
When using `Castable` classes, you may still provide arguments in the `$casts` definition. The arguments will be passed to the `castUsing` method:
788+
When using `Castable` classes, you may still provide arguments in the `casts` method definition. The arguments will be passed to the `castUsing` method:
753789

754790
use App\ValueObjects\Address;
755791

756-
protected $casts = [
757-
'address' => Address::class.':argument',
758-
];
792+
protected function casts(): array
793+
{
794+
return [
795+
'address' => Address::class.':argument',
796+
];
797+
}
759798

760799
<a name="anonymous-cast-classes"></a>
761800
#### Castables & Anonymous Cast Classes

eloquent-serialization.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ You may customize the default serialization format by overriding the `serializeD
204204

205205
You may customize the serialization format of individual Eloquent date attributes by specifying the date format in the model's [cast declarations](/docs/{{version}}/eloquent-mutators#attribute-casting):
206206

207-
protected $casts = [
208-
'birthday' => 'date:Y-m-d',
209-
'joined_at' => 'datetime:Y-m-d H:00',
210-
];
207+
protected function casts(): array
208+
{
209+
return [
210+
'birthday' => 'date:Y-m-d',
211+
'joined_at' => 'datetime:Y-m-d H:00',
212+
];
213+
}

0 commit comments

Comments
 (0)