Skip to main content
InterviewHubPRO
Section Overview
Q23

Question 23 of 50

Part 2: Eloquent ORM, Security & Queue Ecosystem (11 - 24)

46%
Laravel & Ecosystem (50 Qs)•Part 2: Eloquent ORM, Security & Queue Ecosystem (11 - 24)

What are Laravel Custom Casts and Value Objects?

senior LevelCode ExampleSecurity & AuthData Structures

In Simple Words: Allows converting database column values directly into rich Value Objects upon retrieval, and serializing them back upon saving:

php
class MoneyCast implements CastsAttributes {
    public function get($model, string $key, $value, array $attributes): Money {
        return new Money($value, $attributes['currency'] ?? 'USD');
    }
    public function set($model, string $key, $value, array $attributes): array {
        return [$key => $value->amount];
    }
}