Source: juststeveking.uk

Eloquent Attribute Casting
Eloquent Castable attributes are one of the more powerful features of Laravel; some people use them religiously while others tend to shy away from them. You can create a new Laravel project or use an existing one for this tutorial.

So now, if we wanted to extend this at all, we could: namespace App\Casts; use JessArcher\CastableDataTransferObject\CastableDataTransferObject; class Address implements CastableDataTransferObject { public string $nameOrNumber, public string $streetName, public string $localityName, public string $town, public string $county, public string $postalCode, public string $country, public function formatString(): string

namespace App\DataObjects; class Open { public function __construct( public readonly bool $monday, public readonly bool $tuesday, public readonly bool $wednesday, public readonly bool $thursday, public readonly bool $friday, public readonly bool $saturday, public readonly bool $sunday, public readonly array $holidays, ) {}}

Next, we can design our cast: namespace App\Casts; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; class OpenDaysCast implements CastsAttributes { public function __construct( public readonly array $dates, ) {} public function set($model, string $key, $value, array $attributes)
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners