Laravel – Use of Accessors and Mutators to Format Eloquent attributes
In this post we will give you information about Laravel – Use of Accessors and Mutators to Format Eloquent attributes. Hear we will give you detail about Laravel – Use of Accessors and Mutators to Format Eloquent attributesAnd how to use it also give you demo for it if it is necessary.
In this tutorial, I will let you know the use of Accessors and Mutators
to automatically format database fields.
In short term, Accessors are used to format database fields when retrieving from the database and in the same way Mutators are used to format the attributes before saving to the database.
In the secure application, Mutators are commonly used to bcrypt the password before saving into the database.
Defining an AccessorsCombine two attributes to create just one
In this example, i have a “User” model with following fields :
- first_name
- last_name
- password
- created_at
- updated_at
Now we will combine first_name and last_name to get full_name with the help of accessors. We will create accessor method within the model in following way:
- functiongetFullNameAttribute()
- {
- return$this->attributes['first_name'].' '.$this->attributes['last_name'];
- }
function getFullNameAttribute() { return $this->attributes['first_name'] . ' ' . $this->attributes['last_name']; }
Now you can get the full_name in following way :
- $user= AppUser::find(1);// works!
- echo$user->full_name;
$user = AppUser::find(1); // works! echo $user->full_name;
Order Results by Accessors Attribute?
If you want to order results by full_name using orderBy then it will throws an error because the full_name is not real attribute that exist in the database.
- $users= User::orderBy('full_name')->get();// doesn't work
$users = User::orderBy('full_name')->get(); // doesn't work
Solution is very simple to get user list in order using sortBy
and sortByDesc
method.
- $users= User::get()->sortBy('full_name');// works!
- return$users;
$users = User::get()->sortBy('full_name'); // works! return $users;
Defining a Mutators
Mutators use setAttribute
to format field before saving into database.
Capitalize the first name before saving
In this example, We will capitalize the first name with the help of mutators in following way:
- public functionsetFirstNameAttribute($value)
- {
- $this->attributes['first_name']=ucfirst($value);
- }
public function setFirstNameAttribute($value) { $this->attributes['first_name'] = ucfirst($value); }
Hashing Passwords
Never save your password as plain text into your database. Here is quick example to bcrypt the password before save.
- public functionsetPasswordAttribute($value){
- $this->attributes['password']=bcrypt($value);
- }
public function setPasswordAttribute($value) { $this->attributes['password'] = bcrypt($value); }
Accessors and Mutators
will save lot of time when you use it properly in your application.
For example to get created data in human readable format, you can use accessors method.
Hope this code and post will helped you for implement Laravel – Use of Accessors and Mutators to Format Eloquent attributes. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs