I have been using traits extensively in my Eloquent models to segregate related functionalities. For instance, a
User model might incorporate the
HasRole trait, allowing me to define related query scopes, helper methods to determine current user role, and functions to modify the user role.
When you attach a trait to an Eloquent model, Laravel automatically invoke two related methods that can be utilized to alter the model’s behavior. Assuming we are using the
HasRole trait as an example, these two methods will be automatically called:
It follows the pattern of
initialize<TraitName>, as you can see from the underlying source code.
boot<TraitName is a static method and cannot be used to modify the
initialize<TraitName is not, making it the perfect entry point.
Staying consistent with the same
HasRole example, I prefer to push it further by defining all related column inside the trait itself. Assuming the trait requires a column in the
users table called
role to function, I do as follows:
As a bonus point, every property can be modified at this juncture, so I also modify the
$casts property to also recognize
role as castable to the related enum.