3.4 Accessors and Mutators
We sometimes store our data in a different format than how we how we actually use it in our applications—meaning that we have to transform the data every time we retrieve and save it. Accessors and mutators let us automate that process, so that we don't even have to think about transforming our data when we use it.
1.Introduction1 lesson, 01:29
2.The Basics3 lessons, 40:56
3.Using Eloquent5 lessons, 53:38
4.Relationships2 lessons, 26:36
5.Conclusion1 lesson, 00:59
3.4 Accessors and Mutators
We uses databases to store data. Now, I know that is a very obvious statement but bear with me here. So, we put our data in the database and a lot of times that data need to be in certain format. Like for example encrypted text any sensitive information we need to store as encrypted text in the database. That way, if that data ever leaks out then it's at least encrypted, it's not in plain text. But encrypted text really doesn't help us within our application itself. We need to work with the actual data, especially if we are going to display that to the user. Because the user really doesn't care to see the encrypted form of whatever information that they are accessing. So there essentially needs to be a layer between the database and our application. Something that will transform the data so that if it's coming from our database to our application, then it would transform it into something useful within the application. In this particular example, it would unencrypt the data. But then we would need something that would encrypt the data from our application to the database so that it would be stored correctly. And thankfully, with a Laravel applications, that layer is the eloquent model itself. Through what are called accessors and mutators, we can essentially transform the data coming from the database and the data going to the database. So the first thing we're going to do is create a method on our guitar class called stuff. And you can call it whatever you want because we're going to essentially throw this method away. But, every eloquent model has an internal array called attributes. So we want to look at what is inside of this attribute, and really it's quite simple. So let's fire up Tinker, and we want to retrieve an individual guitar so we want to use the find method. Let's just get the one with the ID of 1 and then we're going to call the stuff method. And you can see that we have an array. And all of the attributes are actually the names of the columns within the database. So the reason why I point this out is because with accessors and mutators we essentially access and mutate these attributes. So if we wanted to write an accessor method for the name attributes, we would have to know what that attribute name is. So let's do that, we have quite a few guitars and all of them begin with an uppercase letter and in the case of the names with multiple words every word begins with uppercase letter. So lets say that our application needs to work with these guitar names in all lowercase. So this would be where we could use an accessor to change the case of the name as it comes from the database and we work with it within our application. So, we write an accesser like this. We start it with the term get, this is very easy to remember because we are getting information, or we are accessing information. So get is just another word for access and vice versa. So we say get, and then we have the name of the attribute that we want to get. In this case it is name because that is what we want to retrieve. But if we wanted to work with the createdAt attribute, we would have to say createdAt with no underscore. And it would need to use a Pascal casing in this case, so createdAt, but in this case we just want name. But then we also say attribute. So it's very specific as to how we write these methods, just like the scope methods. The scope methods begin with scope, the accesser methods begin with get. And we don't pass any information to these methods, but eloquent is going to pass a value, the value of the attribute. So this is going to pass the actual name, coming from the database to this method and that's how we work with it inside of this method. So all we want to do here is lowercase that name. So we're going to call the string to lower, we're going to pass in the value and that's going to get us what we want. So if we go back to Tinker, let's fire that back up. And let's retrieve that first guitar, but then let's use just the name property. Now notice, I'm not calling that method, I'm not calling any method. I'm using the name of the attribute, name. And we are going to see the value of vela in all lowercase. This is a little bit more apparent whenever we have multiple names here or multiple words rather. So let's retrieve the guitar with an id of 12, and here we can see that both of these words are in all lowercase. And this is ideal because this way the model is doing all of the work for us. We write just one method, and it does what we want it to do, lowercase all of the names. There is certainly beats having to remember to lowercase the name every time that we wanted to access that data. This way, it's done by the model itself. Well let's write a mutator. Whenever you mutate something, you are changing its state. And the way that you change the state of an object is to set a new value for one or multiple properties. So in this case, the method that we write starts with the term set. So we have get for accessing, we have set for mutating. But then everything else is just like our accessor method, we have the name of the attribute and then the word attribute. Now we do need a value here, because whatever we assign to our name property on our model is going to be parsed to this method here. So we still want that value, and then we essentially just want to set that value for our attribute. Now, we don't want to say this name, because that is going to call the get name attribute, and that is definitely not what we want. Instead, we want to manipulate the attributes array itself, so we say attributes, we pass in the name key and then we set our value. But we are working with lower-cased value and so before we save it to the database we want to upper case it. So let's do that. We will say ucwords, we will pass in the value, and there we go. So let's go to our command-line, let's stop Tinker and start it back up. Let's, once again, get the guitar with an ID of 12. But let's do this, let's store that in the variable, and we will say guitar name. We will see madia brakus like we saw before, but then let's do this. We'll say guitar name = and let's Change it to flying v because that is an actual guitar. It's one that I want to own, but I don't have one yet. So we assigned a new value for the name property. Now let's try to save it, so if we say guitar save, then we see that, that was true. Let's retrieve that guitar once again, and we receive flying v. And if we go to the database, let's refresh this. And whenever we go down to the id of 12, we see that flying v is stored in the database. And it is not lowercase, it was mutated to begin with an uppercase F, and then the V is of course, upper case as well. Now one lesser known thing is that you don't actually have to have an attribute that you specify here. So if we wanted to do something like this, and say function getMyCoolNameAttribute. Now we definitely don't have that in the database. But we could use this as kind of a computed property, so that we could return, really, whatever we want. Let's say that we want to pull in the id as well as the guitar name itself, so this name. Once again, we're going to use the name property here because we are using the lower-cased version of the name within our application. So let's save that and let's clear the screen. Let's call Tinker once again, and let's retrieve the guitar with an ID of 12 and it's name, we see that that is flying v. But then we want to say myCoolAttribute, is that what I called that? Or myCoolName, rather. So we see the ID, colon, and then the name of the guitar. So accessors and mutators aren't just for the attributes or rather the columns in the database. You can write what are essentially your own computed properties and use them within your application. And so, whenever your application needs to work with the data in the database, but it needs to change it in some way, use acessors and mutators. It's one of those features that allows you to write code once and not have to worry about those types of things again.