Skip to content
This repository was archived by the owner on Mar 3, 2025. It is now read-only.

Commit 9a691bc

Browse files
committed
Update README
1 parent 48ecd40 commit 9a691bc

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Address extends CastableDataTransferObject
4444

4545
### 2. Configure your Eloquent attribute to cast to it:
4646

47-
Note that this should be `jsonb` or `json` column in your database schema.
47+
Note that this should be a `jsonb` or `json` column in your database schema.
4848

4949
```php
5050
namespace App\Models;
@@ -60,9 +60,34 @@ class User extends Model
6060
}
6161
```
6262

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.
63+
And that's it! You can now pass either an instance of your `Address` class, or even just an array with a compatible structure. It will automatically be cast between your class and JSON for storage and the data will be validated on the way in and out.
6464

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.
65+
```php
66+
$user = User::create([
67+
// ...
68+
'address' => [
69+
'street' => '1640 Riverside Drive',
70+
'suburb' => 'Hill Valley',
71+
'state' => 'California',
72+
],
73+
])
74+
75+
$residents = User::where('address->suburb', 'Hill Valley')->get();
76+
```
77+
78+
But the best part is that you can decorate your class with domain-specific methods to turn it into a powerful value object.
79+
80+
```php
81+
$user->address->toMapUrl();
82+
83+
$user->address->getCoordinates();
84+
85+
$user->address->getPostageCost($sender);
86+
87+
$user->address->calculateDistance($otherUser->address);
88+
89+
echo (string) $user->address;
90+
```
6691

6792
### Testing
6893

0 commit comments

Comments
 (0)