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
{{ message }}
This repository was archived by the owner on Mar 3, 2025. It is now read-only.
Laravel is awesome. Spatie's data transfer object package for PHP is awesome. They're already good friends, but now they're they're taking their relationship to the next level 💕
8
+
Laravel is awesome. Spatie's [data transfer object](https://github.com/spatie/data-transfer-object) package for PHP is awesome. They're already good friends, but now they're they're taking their relationship to the next level 💕
9
9
10
-
Have you ever wanted to cast your JSON columns to a value object? This package gives you a castable version of Spatie's `DataTransferObject` class, called `CastableDataTransferObject`.
10
+
Have you ever wanted to cast your JSON columns to a value object?
11
+
12
+
This package gives you an extended version of Spatie's `DataTransferObject` class, called `CastableDataTransferObject`.
13
+
14
+
Under the hood it implements Laravel's [`Castable` interface](https://laravel.com/docs/8.x/eloquent-mutators#castables) with a Laravel [custom cast](https://laravel.com/docs/8.x/eloquent-mutators#custom-casts) that handles serializing between the `DataTransferObject` (or a compatible array) and your JSON database column.
11
15
12
16
Check out the blog post at https://jessarcher.com/blog/casting-json-columns-to-value-objects/
Check out readme for Spatie's [data transfer object](https://github.com/spatie/data-transfer-object) package to find out more about what their `DataTransferObject` class can do.
25
31
26
32
```php
27
33
namespace App\Values;
@@ -49,12 +55,14 @@ use Illuminate\Database\Eloquent\Model;
49
55
class User extends Model
50
56
{
51
57
protected $casts = [
52
-
'address' => Address::class
58
+
'address' => Address::class,
53
59
];
54
60
}
55
61
```
56
62
57
-
And that's it!
63
+
And that's it! You can pass either an instance of your `Address` class, or even just an array with a compatible structure. It will automatically be cast to and from your class and JSON for storage.
64
+
65
+
Your data will be validated on the way in and out. But the best part is that you can decorate your class with domain-specific methods to turn it into a powerful value object.
0 commit comments