Google+
Shineyrock web design & consultancy

Shineyrock

blog

  • like 12 08

    Eloquent Mutators and Accessors in Laravel

    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.

    That should create a model file at app/Post.php as shown below.

    Let's replace the contents of that file with the following.

    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.

    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.

    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 an accessor method, you need to provide the get argument when you define your attribute.

    Let's go through the accessor method.

    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.

    Also, you'll need to create an associated route in the routes/web.php file to access the mutator controller.

    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:

    Again, you'll need an associated route in the routes/web.php file to access the accessor controller.

    In the index method, we're using 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 on Envato Market.

    martijn broeders

    founder/ strategic creative at shineyrock web design & consultancy
    e-mail: .(JavaScript must be enabled to view this email address)
    phone: 434 210 0245
  • 1
    silhouette
    mezo
    March 28, 2018 at

    Ι love it whenever people get togetһer and share tһoughts. Ԍreat site, keеp it up!

By - category

    By - date