Accessors & Mutators laravel

What are Mutators?

Mutators are used to modify the data before storing it in DB.

It will use in several scenarios like if we want to capitalize our data or add any other string to it, we can do that with the help of the mutators.

What are Accessors?

Accessors are used to modify the data same as mutators but with the help of accessors, we can modify data while receiving.

Suppose we use an enum in DB, so while retrieving them we can modify it if we want to show the actual value of the enum.

What’s the latest update comes in laravel 9 about Accessor & Mutator?

Now we don’t need to make different methods for accessor & mutator. we can make a single, Non-prefixed method with the help of get & set.


public function address():
 Attribute{    return new Attribute(      
  get: fn ($value, $attributes) => new Address(    
  set: fn (Address $value) => [           
 'address_line_one' => $value->lineOne,

Leave a Reply

Your email address will not be published. Required fields are marked *