Learn about Accessors and Mutators in Laravel

Tram Ho

Introduce

You must never display a combination of data columns from a table or process data before saving it to a database. The answer is probably yes to all developers like us. This is an example where the concepts of Accessor and Mutators are useful. So, let’s find out what these two concepts mean and how they work.

Accessors

Suppose we have two fields in the users table, first_name and last_name, and although these two fields can be used when needed, sometimes we need to combine these two fields to display the full name as A single field, usually the way to get both of these fields would be as follows:

{{ $user->first_name . ' ' . $user->last_name }}

With accessors in Laravel, we can do this by adding an attribute to the model as follows.

{{ $user->full_name }}

As you can see, here we create a function named getFullNameAttribute , which must be written in camelCase style to perform the full name when calling the full_name attribute. So where does this full_name attribute come from? Perhaps reading from the beginning of this article, then you guess that this attribute is created by the getFullNameAttribute function for the User model, ok you guessed it right.

Define an accessors

Through the above example we can understand that, if we want to create an accessors in any model, then our function must have the following rules get[property_name]Attribute , meaning that the function must have good money get to tên thuộc tính and suffix is Attribute .

Obviously Accessors is a very convenient concept when working with models, but the downside is that you cannot use this attribute name in queries with the query builder in Eloquen like any other field, you only have You can use this property when you have a collection (I don’t know how to call it properly, but I don’t think it’s wrong to call a collection).  but not on the queries directly in Eloquent but after we have the fields of the table, you see the example below for clarity.

For example:

  1. $users = User::orderBy('full_name')->get();

The above code will not be able to run.

  1. $users = User::all()->sortBy('full_name');

The code in the second example will work because after having the collection of the user object, we can sort with the full_name property using the sortBy () function.

Mutators

If you’ve worked with any object-oriented language, you’ll be familiar with getter and setter . Therefore, it is possible to view accessor là phương thức getter and mutators là phương thức setter .

Suppose we have a company_name field and want it to be capitalized when stored in the database. We can do that by using the concept of Mutators as follows:

And then we can use like this:

Define a Mutators

Similar to Accessors but vice versa, when defining a Mutators , we declare a function with prefix instead of get as set , followed by property_name and suffix Attribute with function name written in camelCase => function set[property_name]Attribute .

End

These two concepts of Accessors and Mutators are used a lot in Laravel projects, hope this article will bring you useful information, thank you for reading and see you in the next article.

Share the news now

Source : Viblo