Skip to content

Commit db7b86a

Browse files
committed
Merge branch 'castable-docs' of https://github.com/brendt/docs into brendt-castable-docs
2 parents 48f1cbe + be20afd commit db7b86a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

eloquent-mutators.md

+38
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Date Mutators](#date-mutators)
88
- [Attribute Casting](#attribute-casting)
99
- [Custom Casts](#custom-casts)
10+
- [Castables](#castables)
1011
- [Array & JSON Casting](#array-and-json-casting)
1112
- [Date Casting](#date-casting)
1213
- [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
411412

412413
$user->save();
413414

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+
414452
<a name="date-casting"></a>
415453
### Date Casting
416454

0 commit comments

Comments
 (0)