==== Use a custom Eloquent cast to transform a JSON column into a rich PHP Value Object—this gives you encapsulation, immutability and type safety throughout your app. For example: 1. Create your Value Object: ```php final class Address { public function __construct( public string $street, public string $city, public string $zip ) {} } ``` 2. Create a CastsAttributes class: ```php use Illuminate\Contracts\Database\Eloquent\CastsAttributes; class AddressCast implements CastsAttributes { public function get($model, string $key, $value, array $attributes): Address { $data = json_decode($value, true) ?? []; return new Address($data['street'] ?? '', $data['city'] ?? '', $data['zip'] ?? ''); } public function set($model, string $key, $value, array $attributes): array { if ($value instanceof Address) { $payload = [ 'street' => $value->street, 'city' => $value->city, 'zip' => $value->zip, ]; } elseif (is_array($value)) { $payload = $value; } else { throw new InvalidArgumentException('Invalid address format.'); } return [$key => json_encode($payload)]; } } ``` 3. Register the cast in your model: ```php class User extends Model { protected $casts = [ 'address' => AddressCast::class, ]; } ``` Now whenever you access `$user->address`, you get an `Address` object; when you assign one, it’s automatically serialized back to JSON.