What Is Accessors & Mutators In Laravel?

0
391
Laravel Accessor & Mutators

In this tutorial, you’ll learn the way to use laravel accessors and mutators. Keep in mind that, accessors are getters and mutators are setters. With using accessors and mutators you’ll format database column when retrieving or inserting into the database. I’m assuming that you have basic knowledge about retrieving and inserting data with laravel. You can find detailed documentation about accessors and mutators here.

# Requirements

# Defining Accessors

Accessors (getters) are used when you want to reformat column when retrieving data.

Suppose, users table has full_name column. Every time user create an account he fills his name like “John Doe”, “john doe”, “John doe” or “john Doe”. When retrieving and listing that data, you’ll have unstructured column right? You can solve it by using PHP’s ucwords function when retriving this column. We will doing same in accessor.

You will assume that it can be achieved by using an easy ucwords feature, then why make another function known as accessor? Suppose you need to retrive full_name at multiple places and multiple times then? Without accessor, every time you need to consider the usage of ucwords function while retrieving full_name column.

Lets, create accessor through adding a simple function in User model. Check the subsequent getFullNameAttribute function, where full_name column name is transformed to FullName, that is the studly case.

/* app > User.php */

....
public function getFullNameAttribute($value)
{
    return ucwords($value);
}
....

That’s it, you don’t want to remember to apply ucwords everywhere. Accessor will automatically convert full_name column to ucwords, no matter how generally you retrieve data.

# Defining Mutators

Mutators (setters) are used when you want to reformat column when inserting data.

Suppose users table has password column. Every time inserting a password into database you need to encrypt it with your logic right? Laravel uses bcrypt function to encrypt password.

Sometimes, you need to inform your colleague every time to remember that he needs to modify some column name when inserting into the database, and its headache. Accessor & Mutator are a remedy for that.

Lets, create mutator through adding function in User model. Same as accessor, password column will converted into Password. Rememer studly case?

/* app > User.php */

....
public function setPasswordAttribute($value)
{
    $this->attributes['password'] = bcrypt($value);
}
....

From now, whenever any person inserts a password database, it will automatically be encrypted. That’s it! about laravel accessors and mutators. Isn’t it cool trick?

Learn more about Laravel Framework

Previous articleHow To Use Soft Delete In Laravel?
Next articleHow To Generate SSH Key For Git?
Welcome to the world of web development, where technology and creativity come together to bring ideas to life. My name is Keyur Gadher, and as a web development blogger, I am here to share my knowledge and experience with you. From front-end web development to back-end programming, I will provide you with valuable tips, tricks, and techniques to help you create beautiful and functional websites that are tailored to your specific needs. Whether you're a beginner or a seasoned pro, I am here to help you take your web development skills to the next level. Join me today and let's start coding!

LEAVE A REPLY

Please enter your comment!
Please enter your name here