Eloquent model Accessors and Mutators in Laravel

Updated on ... 11th December 2023

Accessors and Mutators In Laravel are Eloquent model features that allow us to manipulate attribute values when fetching data from the database or updating fields in the database. we can set them on Eloquent model instances.

Accessors:

Accessors allow us to format attributes when retrieving them from an Eloquent model(database). 

They are defined as custom methods within the Eloquent model by following the naming convention: get{AttributeName}Attribute. 

Let's understand through an example:

Suppose we have a User model with first_name, middle_name, and last_name attributes, and we want to concatenate these attributes to get the user's full name:


                                                    

                                                    

                                                    namespace App;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    // Accessor method to get the full name attribute
    public function getFullNameAttribute()
    {
        return ucfirst($this->first_name) . ' ' . ucfirst($this->last_name);
    }
}

                                                    

                                                

Now we can access full_name anywhere either in the controller or blade file like other attributes.


                                                    

                                                    

                                                    $user = User::find(1);
echo $user->full_name; // This will output the concatenated full name of the user

                                                    

                                                

You Should Also Read

Mutators:

Mutators allow us to format attribute values before saving them to the database. They are defined as custom methods within the Eloquent model by following a naming convention: set{AttributeName}Attribute. These methods convert the attribute value before it's saved in the database.

For example, suppose you want to hash a password and name first_name first letter should be upper case before storing it in the database, for this you can define it in the user modal:


                                                    

                                                    

                                                    namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;

class User extends Model
{
    // Mutator method to set the hashed password attribute
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }

    public function setFirstNameAttribute($value)
    {
        $this->attributes['first_name'] = ucfirst($value);
    }
}

                                                    

                                                

Now you can use this mutator in the controller from where you are saving or updating the record.

For example:


                                                    

                                                    

                                                    public function store(Request $request){
    $user = new User();
    $user->password = $request->input('password'); // The 'setPasswordAttribute' mutator will hash the password
    $user->first_name = $request->input('first_name'); // The 'setFirstNameAttribute' mutator will capitalize this to 
    $user->save();

}

                                                    

                                                

Note: you have seen that if your accessor or mutator method name is setFirstNameAttribute and getFullNameAttribute then you can access it only using first_name  and full_name.

Related Post

Leave a comment