This content originally appeared on Envato Tuts+ Tutorials and was authored by Sajal Soni
In this article, we'll go through mutators and accessors of the Eloquent ORM in the Laravel web framework. After the introduction, we'll go through a handful of examples to understand these concepts.
In Laravel, mutators and accessors allow you to alter data before it's saved to and fetched from a database. To be specific, the mutator allows you to alter data before it's saved to a database. On the other hand, the accessor allows you to alter data after it's fetched from a database.
In fact, the Laravel model is the central place where you can create mutator and accessor methods. And of course, it's nice to have all your modifications in a single place rather than scattered over different places.
Create Accessors and Mutators in a Model Class
As you're familiar with the basic concept of mutators and accessors now, we'll go ahead and develop a real-world example to demonstrate it.
I assume that you're aware of the Eloquent model in Laravel, and we'll use the Post
model as a starting point of our example. If you haven't created the Post
model yet, let's use the artisan
command to create it.
php artisan make:model Post --migration
That should create a model file at app/Post.php as shown below.
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { // }
Let's replace the contents of that file with the following.
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Casts\Attribute; class Post extends Model { /** * Interact with the user's name attribute. * * @param string $value * @return \Illuminate\Database\Eloquent\Casts\Attribute */ protected function name(): Attribute { return Attribute::make( get: fn ($value) => ucfirst($value), set: fn ($value) => strtolower($value), ); } }
As we've used the --migration
option, it should also create an associated database migration. Just in case you are not aware, you can run the following command so that it actually creates the table in the database.
php artisan migrate
In order to run examples in this article, you need to create the name
column in the post
table. Anyway, we won't go into the details of the migration, since that's out of the scope of this article.
The Mutator Method
Firstly, let's go through the mutator method.
protected function name(): Attribute { return Attribute::make( #get: fn ($value) => ucfirst($value), set: fn ($value) => strtolower($value), ); }
As we discussed earlier, mutators are used to alter data before it's saved to a database. As you can see, the syntax of the mutator method is {attribute-name}
. Of course, you need to replace {attribute-name}
with an actual attribute name in camel case. It's important to note that all accessors and mutators methods return an Attribute
instance which defines how an attribute will be accessed and mutated.
The set
argument is used when the mutator method is called on the attribute. To keep things simple, we've just used the strtolower
function, which converts the post title to lowercase before it's saved to the database.
In this way, you could create mutator methods on all columns of your table. Next, let's go through the accessor method.
The Accessor Method
If mutators are used to alter data before it's saved to a database, the accessor method is used to alter data after it's fetched from a database. To define a accessor method, you need to provide the get
argument when you define your attribute.
Let's go through the accessor method.
protected function name(): Attribute { return Attribute::make( get: fn ($value) => ucfirst($value), #set: fn ($value) => strtolower($value), ); }
The get
argument will be called after the value of the name
attribute is fetched from the database. In our case, we've just used the ucfirst
method to alter the post title.
So far, we've just created mutator and accessor methods, but we'll test them in the upcoming section.
Mutators in Action
Let's create a controller at app/Http/Controllers/MutatorController.php so that we can test the mutator method, which we created in the earlier section.
<?php // app/Http/Controllers/MutatorController.php namespace App\Http\Controllers; use App\Post; use App\Http\Controllers\Controller; class MutatorController extends Controller { public function index() { // create a new post object $post = new Post; $post->name = 'Post Title'; $post->save(); } }
Also, you'll need to create an associated route in the routes/web.php file to access the mutator controller.
Route::get('mutator/index', 'MutatorController@index');
In the index
method, we're creating a new post using the Post
model. It should set the value of the name
column to post title
value since we've used the strtolower
function in the corresponding mutator method.
Accessors in Action
To see accessors in action, let's go ahead and create a controller file app/Http/Controllers/AccessorController.php with the following contents:
<?php namespace App\Http\Controllers; use App\Post; use App\Http\Controllers\Controller; class AccessorController extends Controller { public function index() { // load post $post = Post::find(1); // check the name property, it should be output of the ucfirst function echo $post->name; exit; } }
Again, you'll need an associated route in the routes/web.php file to access the accessor controller.
Route::get('accessor/index', 'AccessorController@index');
In the index
method, we've used the Post
model to load an example post in the first place.
Next, we're inspecting the value of the name
column, and it should start with an uppercase letter as we've already defined the accessor method for that column.
So that's how Eloquent mutators and accessors work!
Conclusion
Today, we've explored the concepts of mutators and accessors of the Eloquent ORM in Laravel. It provides a nice way to alter data before it's saved to and fetched from a database.
For those of you who are either just getting started with Laravel or looking to expand your knowledge, site, or application with extensions, we have a variety of things you can study in Envato Market.
This content originally appeared on Envato Tuts+ Tutorials and was authored by Sajal Soni

Sajal Soni | Sciencx (2018-01-08T16:07:35+00:00) Eloquent Mutators and Accessors in Laravel. Retrieved from https://www.scien.cx/2018/01/08/eloquent-mutators-and-accessors-in-laravel/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.