|
7 | 7 | - [Date Mutators](#date-mutators)
|
8 | 8 | - [Attribute Casting](#attribute-casting)
|
9 | 9 | - [Custom Casts](#custom-casts)
|
| 10 | + - [Castables](#castables) |
10 | 11 | - [Array & JSON Casting](#array-and-json-casting)
|
11 | 12 | - [Date Casting](#date-casting)
|
12 | 13 | - [Query Time Casting](#query-time-casting)
|
@@ -411,6 +412,43 @@ Once the cast is defined, you may access the `options` attribute and it will aut
|
411 | 412 |
|
412 | 413 | $user->save();
|
413 | 414 |
|
| 415 | +<a name="castables"></a> |
| 416 | +### Castables |
| 417 | + |
| 418 | +Instead of using custom cast classes, you can also make use of the `Castable` interface. Value objects who implement it can be used directly in the `$casts` configuration. |
| 419 | + |
| 420 | + protected $casts = [ |
| 421 | + 'options' => \App\ValueObjects\Address::class, |
| 422 | + ]; |
| 423 | + |
| 424 | +By implementing the `Castable` interface, you'll need to specify the caster class in the value object. |
| 425 | + |
| 426 | + <?php |
| 427 | + |
| 428 | + namespace App\ValueObjects; |
| 429 | + |
| 430 | + use Illuminate\Contracts\Database\Eloquent\Castable; |
| 431 | + use App\Casts\Address as AddressCast; |
| 432 | + |
| 433 | + class Address implements Castable |
| 434 | + { |
| 435 | + /** |
| 436 | + * Get the caster class for this class. |
| 437 | + * |
| 438 | + * @return string |
| 439 | + */ |
| 440 | + public static function castUsing() |
| 441 | + { |
| 442 | + return AddressCast::class; |
| 443 | + } |
| 444 | + } |
| 445 | + |
| 446 | +You can still pass arguments in the `$casts` configuration, they are passed directly to the caster class. |
| 447 | + |
| 448 | + protected $casts = [ |
| 449 | + 'options' => \App\ValueObjects\Address::class . ':argument', |
| 450 | + ]; |
| 451 | + |
414 | 452 | <a name="date-casting"></a>
|
415 | 453 | ### Date Casting
|
416 | 454 |
|
|
0 commit comments