---
title: "What are Accessors and Mutators in Laravel? Learn How to Use Them"
url: "https://magecomp.com/blog/accessors-and-mutators-in-laravel-how-to-use-them/"
date: "2023-03-09T17:33:56+00:00"
modified: "2025-01-06T09:59:02+00:00"
author:
  name: "Bharat Desai"
  url: "https://magecomp.com"
categories:
  - "Laravel"
word_count: 639
reading_time: "4 min read"
summary: "Hello Laravel Friends,"
description: "Accessors and mutators are used to format Eloquent attributes in Laravel. Learn more about Accessors and Mutators and how to use them"
keywords: "Laravel"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Laravel Tinker: Quick and Easy Command Line Interactions"
    url: "https://magecomp.com/blog/laravel-tinker/"
  - title: "How to Use Flash Messages in Laravel 12 with Example"
    url: "https://magecomp.com/blog/use-flash-messages-laravel-12/"
  - title: "How to Add Custom Attributes in Laravel Model?"
    url: "https://magecomp.com/blog/add-custom-attributes-in-laravel-model/"
---

# What are Accessors and Mutators in Laravel? Learn How to Use Them

_Published: March 9, 2023_  
_Author: Bharat Desai_  

![What are Accessors and Mutators in Laravel](https://magecomp.com/blog/wp-content/uploads/2023/03/What-are-Accessors-and-Mutators-in-Laravel1-1024x512.webp)

**Hello Laravel Friends,**

In this Laravel tutorial, we will learn about Accessors and Mutators in Laravel. We will also go through the examples to understand the concept of how to use Accessors and Mutators in Laravel.
So let’s see Laravel 8 Accessor and Laravel 8 Mutator with examples.
[![Laravel Development Services](https://magecomp.com/blog/wp-content/uploads/2024/12/Laravel-Development-Services-4.webp)](https://magecomp.com/services/laravel-development-services/)

## What are Accessors and Mutators in Laravel?
Basically, Laravel Accessors and Mutators are custom, user-defined methods. Accessors and mutators are used to format Eloquent attributes when retrieving them from a model or setting their value.
Accessors are used to alter the attributes when you retrieve them from the database. Whereas, Mutators are used to alter the attributes before saving them to the database.
### Accessors
To define an accessor, create a get{Attribute_name}Attribute method on your model where Attribute_name is the name of the column you wish to access.
For example, we are creating a method getCreatedAtAttribute() for casting a created_at dateTime attribute of the Comment Model in human-readable form.
Add the below code to the model
path – **App/Model/Comment**

```
/**
     * Get the comment create time in human-readable form.
     *
     * @param datetime $value
     * @return string
*/
public function getCreatedAtAttribute($value)
{
        return Carbon::parse($value)->diffForHumans([ 'parts' => 1]);
}
```

How to use > Comment::find(1)->created_at;
Model code – Your comment model looks like this
```
<?php
namespace AppModels;
use CarbonCarbon;
use CarbonCarbonInterface;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Comment extends Model
{
    use HasFactory;
    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'comment',
        'user_id',
        'post_id',
    ];

    /**
     * Get the comment create time in human-readable form.
     *
     * @param datetime $value
     * @return string
     */
    public function getCreatedAtAttribute($value)
    {
        return Carbon::parse($value)->diffForHumans(['syntax' => CarbonInterface::DIFF_ABSOLUTE]);
    }
}
```

**Output**

```
// 6 months ago
```

### [![Hire laravel Developer](https://magecomp.com/blog/wp-content/uploads/2024/12/Hire-Laravel-Expert-Now-3.webp)](https://magecomp.com/services/hire-laravel-developer/)

### Mutator
To define a mutator, define a set{Attribute_name}Attribute method on your model where Attribute_name is the name of the column you wish to access.
For example, we are creating a method setFirstNameAttribute() for the first_name attribute of the User Model to store value in lowercase.
Path –  **App/Model/User**

```
/**
     * Set the user's first name.
     *
     * @param  string  $value
     * @return void
*/
public function setFirstNameAttribute($value)
{
        $this->attributes['first_name'] = strtolower($value);
}
```

How to use >
```
$user = User::create([
            'first_name' => "MageComp",
            "last_name" => "LLP",
            "email" => "mailto:magecomp@mail.com",
            "password" => Hash::make("password")
        ]);
return $user;
```

The user Model Looks like this
```
<?php
namespace AppModels;
use CarbonCarbon;
use CarbonCarbonInterface;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
use LaravelSanctumHasApiTokens;
class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'first_name',
        'last_name',
        'email',
        'password',
    ];
    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];
    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    /**
     * Set the user's first name.
     *
     * @param  string  $value
     * @return void
     */
    public function setFirstNameAttribute($value)
    {
        $this->attributes['first_name'] = strtolower($value);
    }
}
```

**Output**

```
// {
//     "id":1,
//     "first_name":"magecomp", // to lowercase
//     "last_name":"LLP",
//     "mailto:email":"magecomp@mail.com",
//     "email_verified_at":"2023-03-09T10:09:10.000000Z",
//     "created_at":"2022-03-09T10:09:10.000000Z",
//     "updated_at":"2022-03-09T05:24:33.000000Z"
// }
```

## Conclusion:
This was all about Accessors and Mutators in Laravel. Accessors and Mutators provide a great way to alter data before it’s saved or retrieved from a database.
If you are looking to expand your knowledge in Laravel, check out some of the important [**Laravel tutorials**](https://magecomp.com/blog/category/laravel/) that will help you learn deeply about Laravel.
Share this guide on Accessors and Mutators in Laravel with your friends to help them learn about these concepts.
[![Contact Us](https://magecomp.com/blog/wp-content/uploads/2024/12/Contact-Us-3.webp)](https://magecomp.com/contact)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/accessors-and-mutators-in-laravel-how-to-use-them/](https://magecomp.com/blog/accessors-and-mutators-in-laravel-how-to-use-them/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-09 08:47:29 UTC_  
