Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add castable docs #5934

Merged
merged 2 commits into from
Apr 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions eloquent-mutators.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Date Mutators](#date-mutators)
- [Attribute Casting](#attribute-casting)
- [Custom Casts](#custom-casts)
- [Castables](#castables)
- [Array & JSON Casting](#array-and-json-casting)
- [Date Casting](#date-casting)
- [Query Time Casting](#query-time-casting)
Expand Down Expand Up @@ -411,6 +412,43 @@ Once the cast is defined, you may access the `options` attribute and it will aut

$user->save();

<a name="castables"></a>
### Castables

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.

protected $casts = [
'options' => \App\ValueObjects\Address::class,
];

By implementing the `Castable` interface, you'll need to specify the caster class in the value object.

<?php

namespace App\ValueObjects;

use Illuminate\Contracts\Database\Eloquent\Castable;
use App\Casts\Address as AddressCast;

class Address implements Castable
{
/**
* Get the caster class for this class.
*
* @return string
*/
public static function castUsing()
{
return AddressCast::class;
}
}

You can still pass arguments in the `$casts` configuration, they are passed directly to the caster class.

protected $casts = [
'options' => \App\ValueObjects\Address::class . ':argument',
];

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

Expand Down