Check if a Mutator or Accessor has been set

Little Known Laravel — Tip #3

This is tip #3 in a series covering lesser known Laravel tips. It’s my hope that each tip helps you in your development journeys.

In an Eloquent Model you can set mutators & accessors on your Model properties. There may be times where you might need to check if a mutator or accessor exists for this property.

For example we have an accessor on the cheese property of our Pizza Model, this will return a boolean result:

// Retrieve a model by its primary key...
$pizza = App\Pizza::find(1);// Returns boolean result based on accessor existence
echo $pizza->hasSetMutator('cheese');

We can also have a mutator on the same cheese property that we want to check against:

// Retrieve a model by its primary key...
$pizza = App\Pizza::find(1);// Returns boolean result based on accessor existence
echo $pizza->hasGetMutator('cheese');

If you found this tip helpful, feel free to follow me on Twitter @smayzes and please consider sharing!

20

20 claps
Shawn Mayzes

Written by

The more I think about what to put here, the more I procrastinate. Founder: @larachatslack