In our previous post, we learnt what are accessors and mutators, how can they be helpful and how to write them in our Models. Let's now understand how to check if an accessor or mutator exists for an attribute.
Taking an example from previous post, we have an accessor on FirstName
attribute of our Employee model. In order to check if an accessor exists, we need to use hasGetMutator
method as follows:
$employee = Employee::find(1);
echo $employee->hasGetMutator('first_name'); // Returns boolean based on existence of an accessor
Similarly, we can check whether a mutator hasSetMutator
exists for an attribute as follows:
$employee = Employee::find(1);
echo $employee->hasSetMutator('first_name'); // Returns boolean based on existence of a mutator
Hope this helped you. Happy Coding! :)