Laravel – Use of Accessors and Mutators to Format Eloquent attributes

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

See also  google Invisible reCAPTCHA v2 using php - php Invisible reCAPTCHA

In this example, i have a “User” model with following fields :

  • first_name
  • last_name
  • email
  • 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:

  1. functiongetFullNameAttribute()
  2. {
  3. return$this->attributes['first_name'].' '.$this->attributes['last_name'];
  4. }
function getFullNameAttribute()
  return $this->attributes['first_name'] . ' ' . $this->attributes['last_name'];

Now you can get the full_name in following way :

  1. $user= AppUser::find(1);// works!
  2. 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.

  1. $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.

  1. $users= User::get()->sortBy('full_name');// works!
  2. return$users;
$users = User::get()->sortBy('full_name'); //  works!
return $users;

Defining a Mutators

See also  PHP curl post request with parameters and get json response

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:

  1. public functionsetFirstNameAttribute($value)
  2. {
  3. $this->attributes['first_name']=ucfirst($value);
  4. }
 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.

  1. public functionsetPasswordAttribute($value){
  2. $this->attributes['password']=bcrypt($value);
  3. }
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.

See also  How to Strip All Spaces Out of a String in PHP

Label :


Laravel PHP Framework

How To


Web Development

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

For More Info See :: laravel And github

Leave a Comment

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

39  +    =  48

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US