Laravel : Accessors and mutators — Learn how to use them

Vipul Basapati
3 min readNov 29, 2017

--

Have you ever had to show a combination of columns of data from table or process data before saving it to the database. The answer would probably be yes, who doesn’t. This is where the concept of Accessors and mutators come in handy.

Let me show you what they mean and how they work.

Accessors :

Suppose we have two fields in our table for our users : first_name and last_name. And though these two fields can be used when needed, but sometimes we need these two combined to show the full name as a single field, have operations on them as a whole.

The Naïve way :

{{ $user->first_name . ' ' . $user->last_name }}

The Laravel Way :

{{ $user->full_name }}

The laravel way can be achieved by adding an attribute function in the model.

And we can do it like this :

public function getFullNameAttribute()	 	 
{
return $this->first_name . " " . $this->last_name;
}

So what’s happening here is, we have created a function which will get called every time full_name is called on the user( Model ). There is a pattern, if you see closely, in every attribute function you make. The get[attribute_name]Attribute(), is the way you can create an accessor.

But there is a catch, you can not use this attribute name in eloquent queries like any other fields, you can only use this attribute when you have the collection, but not on the eloquent queries as you might know, the eloquent queries work on the database fields, and our attribute comes in the scene, after we have all the fields of the table.

For example,

$users = User::orderBy('full_name')->get();

The above code would not work.

But,

$users = User::all()->sortBy('full_name');

The above would work as sortBy() requires a collection to work on. So after getting the collection of users, we can then sort it by using sortBy()function.

Note: There is a sortByDesc() that would sort the collection by descending order.

Mutators :

If you have worked with any OOP language, you would be familiar with getter and setter methods. So, the accessors are the getter and mutators are the setter method.

Being said that, suppose we have a field company_name and want this to be capitalized when stored into database. We can achieve that using mutators.

public function setCompanyNameAttribute($value)
{
$this->attributes['company_name'] = strtoupper($value);
}

And can be used like this :

$user->company_name = Input::get('company');$user->save();

In Laravel, every model object has two values original and attributes and by changing any value in the collection, we actually change the attribute value and that is what is getting stored in the database or shown in the blade or whatever way you use it.

I hope you like this post.

Laravel And JS Resources

Below are some resources for Laravel and Javascript and my favorite JS framework: React.

Laravel

  1. Official Docs are the best maintained for Laravel. And Laracasts is the best.

Javascript

  1. For learning Javascript, Wes Bos is the best one to teach, because I have taken at least 6 courses from him. And have everything positive. You can learn the basics of JS and get your hands-on Vanilla Javascript using JavaScript30 course.
  2. You can learn the new syntax or the modern Javascript syntax called ES6 from es6.io. (Paid)
  3. You can learn React from scratch by learning from this tutorial on ReactForBeginners.com. (Paid)

--

--