Laravel has great eloquent feature accessors and mutators to set and get the value from the database with the alteration.
It is mean that these eloquent accessors and mutators methods allow us to alter the data before saving it to the database and get from the database.
So in this tutorial, we will learn about eloquent accessors and mutators methods and how we can use them in the latest version of laravel 9.
Let’s get started to understand the accessors and mutators methods in laravel eloquent with the help of examples.
Install Laravel Application
You can skip this step if you have already installed the laravel, if not then run the following command to install the laravel of the latest distribution.
After installation, set up the database configuration, and then you have to make the migration file to add the columns that you want in the table. Let’s make the migration file for the student table, so run the following command.
The above command will make the migration file and now add the fields. For this tutorial we are adding “name”, “roll_no”, “section”, “address”, “mark_sheet”, etc.
From one of these fields, we will use to make a mutator and accessor method in this tutorial example. We will use laravel 9 syntaxes to make mutators and accessors methods in an eloquent model.
To make a model in laravel run the following command.
See Also: Create Laravel Model, Migration, Controller and Resources in One Command
See Also: Create Laravel 9 CRUD Application
Eloquent: Mutator Method
Let’s discuss the mutator method first then we will see the accessor method in eloquent and how we can create them.
The mutator method is used to transform the eloquent attribute value when it is inserted into the database. You can simply say that it is used to set the custom behavior of the data field while inserting it into the database table.
To make a mutator method in a model, you have to make a function and set the attribute value with a set
argument.
The name of the mutator method must be an attribute name with a camel case convention and referred to as it is attributed. To better understand see the following example code.
In the above example code, we took the Student
model class for reference and create the markSheet
mutator method to alter the values of the mark_sheet field.
What will the markSheet()
mutator method do???
So this mutator method will convert the mark_sheet field value from array to JSON object before saving it into the database.
Eloquent: Accessor Method
In the above example, you see how to alter the value using the mutator method before saving it into the database.
Now we will learn about what is accessor method in eloquent and how to make it and what the purpose this?
To make the accessor method you have to use the get
argument instead set
. The syntax of defining the method is the same. The accessor method is used to transform the value before accessing it from the database.
Let’s take the same above example. As we stored the mark_sheet
field values in a JSON object in the database using the mutator and now we want to fetch that data as an array.
So for this, we have to make the accessor method for the mark_sheet
field and use the get argument with converting the value into an array.
So in the above example, we made an accessor method markSheet()
and convert the value of this field before getting it.
Enable Accessor Cache
If you want to enable the cache for specific values then you can use the shouldCache()
method when you are defining the accessor method.
Disable Accessor Cache
To disable the object cache for attribute values in accessors methods, you should use withoutObjectCaching()
method. It will disable the object cache for the accessor attribute.
Mutator and Accessor Together
If you are making mutators and accessors eloquent methods for the same attribute of model then you set and get the value in one function.
Conclusion
So in this tutorial, you learn about the eloquent features of mutators and accessors and what is the syntax of making them in the latest version laravel 9.
In brief, you see how to make a mutator method in laravel model to change the value of the data field before saving it into the database.
And accessor method is used to transform the data field value before fetching it from the database. And you can get the values with cache and without cache.