You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -196,9 +196,9 @@ protected function address(): Attribute
196
196
<aname="attribute-casting"></a>
197
197
## Attribute Casting
198
198
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.
200
200
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:
202
202
203
203
<divclass="content-list"markdown="1">
204
204
@@ -237,13 +237,16 @@ To demonstrate attribute casting, let's cast the `is_admin` attribute, which is
237
237
class User extends Model
238
238
{
239
239
/**
240
-
* The attributes that should be cast.
240
+
* Get the attributes that should be cast.
241
241
*
242
-
* @var array
242
+
* @return array<string, string>
243
243
*/
244
-
protected $casts = [
245
-
'is_admin' => 'boolean',
246
-
];
244
+
protected function casts(): array
245
+
{
246
+
return [
247
+
'is_admin' => 'boolean',
248
+
];
249
+
}
247
250
}
248
251
249
252
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
279
282
class User extends Model
280
283
{
281
284
/**
282
-
* The attributes that should be cast.
285
+
* Get the attributes that should be cast.
283
286
*
284
-
* @var array
287
+
* @return array<string, string>
285
288
*/
286
-
protected $casts = [
287
-
'directory' => AsStringable::class,
288
-
];
289
+
protected function casts(): array
290
+
{
291
+
return [
292
+
'directory' => AsStringable::class,
293
+
];
294
+
}
289
295
}
290
296
291
297
<aname="array-and-json-casting"></a>
@@ -302,13 +308,16 @@ The `array` cast is particularly useful when working with columns that are store
302
308
class User extends Model
303
309
{
304
310
/**
305
-
* The attributes that should be cast.
311
+
* Get the attributes that should be cast.
306
312
*
307
-
* @var array
313
+
* @return array<string, string>
308
314
*/
309
-
protected $casts = [
310
-
'options' => 'array',
311
-
];
315
+
protected function casts(): array
316
+
{
317
+
return [
318
+
'options' => 'array',
319
+
];
320
+
}
312
321
}
313
322
314
323
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
345
354
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
346
355
347
356
/**
348
-
* The attributes that should be cast.
357
+
* Get the attributes that should be cast.
349
358
*
350
-
* @var array
359
+
* @return array<string, string>
351
360
*/
352
-
protected $casts = [
353
-
'options' => AsArrayObject::class,
354
-
];
361
+
protected function casts(): array
362
+
{
363
+
return [
364
+
'options' => AsArrayObject::class,
365
+
];
366
+
}
355
367
356
368
Similarly, Laravel offers an `AsCollection` cast that casts your JSON attribute to a Laravel [Collection](/docs/{{version}}/collections) instance:
357
369
358
370
use Illuminate\Database\Eloquent\Casts\AsCollection;
359
371
360
372
/**
361
-
* The attributes that should be cast.
373
+
* Get the attributes that should be cast.
362
374
*
363
-
* @var array
375
+
* @return array<string, string>
364
376
*/
365
-
protected $casts = [
366
-
'options' => AsCollection::class,
367
-
];
377
+
protected function casts(): array
378
+
{
379
+
return [
380
+
'options' => AsCollection::class,
381
+
];
382
+
}
368
383
369
384
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:
370
385
371
386
use App\Collections\OptionCollection;
372
387
use Illuminate\Database\Eloquent\Casts\AsCollection;
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.
387
405
388
406
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):
389
407
390
408
/**
391
-
* The attributes that should be cast.
409
+
* Get the attributes that should be cast.
392
410
*
393
-
* @var array
411
+
* @return array<string, string>
394
412
*/
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
+
}
398
419
399
420
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.
400
421
@@ -427,18 +448,21 @@ If a custom format is applied to the `date` or `datetime` cast, such as `datetim
427
448
<aname="enum-casting"></a>
428
449
### Enum Casting
429
450
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:
431
452
432
453
use App\Enums\ServerStatus;
433
454
434
455
/**
435
-
* The attributes that should be cast.
456
+
* Get the attributes that should be cast.
436
457
*
437
-
* @var array
458
+
* @return array<string, string>
438
459
*/
439
-
protected $casts = [
440
-
'status' => ServerStatus::class,
441
-
];
460
+
protected function casts(): array
461
+
{
462
+
return [
463
+
'status' => ServerStatus::class,
464
+
];
465
+
}
442
466
443
467
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:
444
468
@@ -457,13 +481,16 @@ Sometimes you may need your model to store an array of enum values within a sing
457
481
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
@@ -555,13 +582,16 @@ Once you have defined a custom cast type, you may attach it to a model attribute
555
582
class User extends Model
556
583
{
557
584
/**
558
-
* The attributes that should be cast.
585
+
* Get the attributes that should be cast.
559
586
*
560
-
* @var array
587
+
* @return array<string, string>
561
588
*/
562
-
protected $casts = [
563
-
'options' => Json::class,
564
-
];
589
+
protected function casts(): array
590
+
{
591
+
return [
592
+
'options' => Json::class,
593
+
];
594
+
}
565
595
}
566
596
567
597
<aname="value-object-casting"></a>
@@ -708,13 +738,16 @@ A classic example of an inbound only cast is a "hashing" cast. For example, we m
708
738
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:
709
739
710
740
/**
711
-
* The attributes that should be cast.
741
+
* Get the attributes that should be cast.
712
742
*
713
-
* @var array
743
+
* @return array<string, string>
714
744
*/
715
-
protected $casts = [
716
-
'secret' => Hash::class.':sha256',
717
-
];
745
+
protected function casts(): array
746
+
{
747
+
return [
748
+
'secret' => Hash::class.':sha256',
749
+
];
750
+
}
718
751
719
752
<aname="castables"></a>
720
753
### Castables
@@ -723,9 +756,12 @@ You may want to allow your application's value objects to define their own custo
723
756
724
757
use App\ValueObjects\Address;
725
758
726
-
protected $casts = [
727
-
'address' => Address::class,
728
-
];
759
+
protected function casts(): array
760
+
{
761
+
return [
762
+
'address' => Address::class,
763
+
];
764
+
}
729
765
730
766
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:
731
767
@@ -749,13 +785,16 @@ Objects that implement the `Castable` interface must define a `castUsing` method
749
785
}
750
786
}
751
787
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:
Copy file name to clipboardExpand all lines: eloquent-serialization.md
+7-4Lines changed: 7 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -204,7 +204,10 @@ You may customize the default serialization format by overriding the `serializeD
204
204
205
205
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):
0 commit comments