the "classic" way would be a getAddressAttribute() method that is accessible via ->address.
The only difference/advantage of the new way that I see comes when defining getters and setters for an attribute in the same place.
Take a look at these two methods:
// Laravel's way of defining an Attribute
protected function address(): Attribute
{
return Attribute::make(
get: fn (mixed $value, array $attributes) => new Address(
$attributes['address_line_one'],
$attributes['address_line_two'],
),
);
}
// The classic way (like in other OO languages), that probably everyone knows
public function getAddress(): Address
{
return new Address(
$this->address_line_one,
$this->address_line_two,
);
}
Both are doing basically the same thing. The first one is accessable with $model->address, while the second one is callable through $model->getAddress().
I know Laravel's Attribute comes with caching if enabled. I currently see only the caching as an advantage and that this is accessable like an actual model attribute.
Any other advantages that I can't think of right now?
Or tu put it another way: Anything against a classic getAddress() method here?
Thanks for your ideas!
Please or to participate in this conversation.