Laravel : Accessors and mutators — Learn how to use them

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.


$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. Find more posts here : Laravel.

Vipul kumar basapati

Written by

Tech Enthusiast, learner by birth. Workout with @laravel and @react and @node

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade