New way to define Accessors and Mutators in Laravel!
2 min readDec 28, 2021
As you know there is a way to define accessor and mutator in laravel like below.
Laravel 9 Series:
- Episode 1: Laravel 9: Stub Migrations
- Episode 2: Laravel 9: New Designs
- Episode 3: Laravel 9: New Helpers
- Episode 4: Laravel 9: Enums!
- Episode 5: Laravel 9: Full Text Index & SwiftMailer
For example if we are in user model (App\Models\User):
For Getter:
For Settter:
There is a new way to achieve this scenario But I must say Taylor Otwell actually mentioned it and it is quite useful and nice.
You can define both setter and getter in one single method like below:
and that would do it as another way to define accessors and mutators both in one attribute function. If don’t want to write any of these you can simply pass a null value for get/set.
Lets see two more examples for user model to cracked it down:
That’s it… any questions?